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.
$this -> render(‘edit’);
So the code above will output the “accounts/edit.ctp” view file.







August 22nd, 2008 at 8:34 am
Thanks for your support!
August 26th, 2008 at 3:46 pm
thanks for pointing out, I was looking for that info
September 18th, 2008 at 9:47 pm
Thanks. This is exactly what I am looking for.
January 20th, 2009 at 1:23 pm
This is exactly what I’m looking for. Thanks.
February 13th, 2009 at 11:16 am
hell yeah, thanx!
February 15th, 2009 at 9:43 pm
can i use redirect() to do it??
i have difficulty to pass a param to other controller’s view function.
when i do
$this->redirect(‘accounts/view/?id=2′)
the program will render to index function??
can u give me some hints??
thx!!!
February 16th, 2009 at 5:00 pm
Don’t use “?id=2″
You can try something like…
$this -> redirect('accounts/view/2/');…then debug $this -> params and see what you get.
July 9th, 2009 at 11:27 pm
Goood! Short ‘n sweet!
July 21st, 2009 at 2:56 am
how can we set for another view folder.
i want to set a new view folder like ‘newview’.
from where i can set it?
and how i can use it????
November 10th, 2009 at 10:44 pm
Can update http://book.cakephp.org ??
There is no information about viewPath.
December 9th, 2009 at 3:40 am
hey dude this is very useful information exactly what i was looking for, i was stuck when i need to create multiple forms for the same page…noe i can place different forms in separate views and select the views accordingly based on condition
thanks
January 22nd, 2010 at 8:56 pm
You also can use
$this->render(‘/folder/file_without_ctp_extension’);
February 22nd, 2010 at 8:29 pm
It doestn load the layout this way!
March 16th, 2010 at 8:08 am
In stead of:
$this -> viewPath = ‘accounts’;
$this -> render(‘edit’);
you can:
$this -> render(‘../accounts/edit’);