CakePHP : Change view file from controller

With CakePHP, the controller core automatically detects the file name of the view file which needs to be rendered from a controller action. You might want to reuse an existent view for an action or specify a different file name.

For example, if you have a controller named “Users” and you execute a controller action named “profile”, the view file “users/profile.ctp” will be automatically rendered. But you might want to output a view file named “edit.ctp” in the “users” folder. You can do this with the render method.

The code above will automatically render the “users/edit.ctp” view file after the controller action/method has been executed. But say for example that your “edit.ctp” file is not within the “users” folder, but rather inside a folder named “accounts”. Then you can change the viewPath and then execute the render method.

So the code above will output the “accounts/edit.ctp” view file.