Sriniwas
Sriniwas

Reputation: 515

CakePHP : Multiple views from One Controller

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..

Upvotes: 0

Views: 2561

Answers (3)

Tahmina Khatoon
Tahmina Khatoon

Reputation: 1091

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.

Upvotes: 2

Spoke
Spoke

Reputation: 96

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.

Upvotes: 1

Alvaro
Alvaro

Reputation: 41605

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 :)

Upvotes: 0

Related Questions