CakePHP : Change view file from controller

Sat, Aug 9, 2008

CakePHP, PHP

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.

$this -> render(‘edit’);

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 -> viewPath = ‘accounts’;
$this -> render(‘edit’);

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

, , , , ,
banner-rotator-widejpg

This post was written by:

- who has written 73 posts on Lost-In-Code.

I (Antonie Potgieter) am a software engineer/web developer located in South Africa. My full-time work is the management of Tribulant Software and the development of its software packages.

Contact the author

20 Comments For This Post

  1. retomi Says:

    Thanks for your support!

  2. yaqoob Says:

    thanks for pointing out, I was looking for that info

  3. Smart Pad Says:

    Thanks. This is exactly what I am looking for.

  4. nting Says:

    This is exactly what I’m looking for. Thanks.

  5. _dan Says:

    hell yeah, thanx!

  6. joe Says:

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

  7. Antonie Potgieter Says:

    Don’t use “?id=2″
    You can try something like…

    $this -> redirect('accounts/view/2/');

    …then debug $this -> params and see what you get.

  8. Viktor Says:

    Goood! Short ‘n sweet! :)

  9. Sachin Says:

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

  10. mul14 Says:

    Can update http://book.cakephp.org ??
    There is no information about viewPath.

  11. phpbookstore Says:

    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

  12. TEHEK Says:

    You also can use

    $this->render(‘/folder/file_without_ctp_extension’);

  13. Michael Says:

    It doestn load the layout this way!

  14. Richard@Home Says:

    In stead of:

    $this -> viewPath = ‘accounts’;
    $this -> render(‘edit’);

    you can:

    $this -> render(‘../accounts/edit’);

  15. Raven Says:

    cakephp is awesome

  16. RSK Says:

    if u want to pass any variable to view
    Then $this->set(compact(‘someVariable’));
    (Set) statement should be given before calling render().

  17. vancouver web design Says:

    I guess it would ignore default view, and view variables would work as normal?

  18. Haris Says:

    Little help please!!! Lets say you have a posts slider that rotates e.g. the last 4 posts in the page banner and you also want to loop over the latest posts in the content area of the page that limits lets say 10 to 20 latest posts. Is there a way that i could create another method and send it to the view to render the slider separately from the index action?

    Thanks in advance…

  19. Shmeksi Says:

    Good work !

  20. Raju Kumar Says:

    nice information

1 Trackbacks For This Post

  1. [PRONIQUE] CakePHP Developer Links Says:

    [...] http://www.lost-in-code.com/…/cakephp-change-view-file-from-controller/ [...]

Leave a Reply