0

I am working in CakePHP for the 1st time. I need to create multiple views for a single controller.
Eg: I have a settings table.

Schema of settings table

1.ID
2.Name
3.Type

I have created its model and controller using cake bake. But i have multiple views from where the data goes into the settings table. My data of designations, departments, qualifications, projects and many other things go into the type field of the settings table with their names as entered.

So when i m creating the model and controller thru cake bake it is creating view as per the settings table, whereas i need view pages as per types, i.e Create Designation, Create Departments, Create Projects and also view, edit and delete files for them.

Pls help me find a way to achieve that..

3 Answers 3

2

I think you are looking for

$this->render('viewfilename');

create as many views as you want and based on your requirement send then in specific view from controller.

For example:

public function add($type) {
    if ($this->request->is('post')) {
        ...
    }

    $this->set(............);

    switch ($type) {
        case 'designations':
            $this->render('add_designations');
            break;
        case 'departments':
            $this->render('add_departments');
            break;
        case 'qualifications':
            $this->render('add_qualifications');
            break;
    }

}

and make view files as add_designations.ctp, add_departments.ctp, add_qualifications.ctp etc in view folder.

Sign up to request clarification or add additional context in comments.

1 Comment

If I've pagesController in controllers directory, I should create those 3 files inside Pages directory inside view folder?
1

You can add Views by creating a .ctp file in the respective Views Folder (Views/"Modelname"/add_department.ctp)

In your "Modelname" Controller you just add

function addDepartment() {
    // Logic here
}

But if you just want to set the type, you can create a normal add.ctp and create a Selectbox with all the different possible Types.

Comments

0

You need to read again how the pattern Model View Controller (MVC) works.

If you want to create a new department, you probably want to use the departmentsController associated with the Department model.

In each controller you will have the actions associated with it. This way Cake Bake will generate the add, delete and edit code for each of your controllers.

Of course, you can create them by your own in the controller you prefer making use of the model you wish. But don't expect Cake bake to work differently :)

3 Comments

thats what the issue is.. my department needs to be set in the setting table first. i.e If i want to create a Sales department i need do do that in the settings table. Hence i need different views for different categories. How can I achieve it?
If you have a table in your database in which you save all the data related to departments (usually called, department), you should them work with the departmentsController and if you need to modify the setting table you just call the Setting model from your departmentsController making use of var uses
Remember that a controller can use multiple models.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.