CakePHP View including other views

后端 未结 7 2165
盖世英雄少女心
盖世英雄少女心 2021-02-07 12:22

I have a CakePHP application that in some moment will show a view with product media (pictures or videos) I want to know if, there is someway to include another view that threat

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-07 12:38

    In your controller (in this example the posts controller).

    function something() {
        return $this->Post->find('all');
    }
    

    In your elements directory (app/views/element) create a file called posts.ctp.

    In posts.ctp:

    $posts = $this->requestAction('posts/something'); 
    foreach($posts as $post): 
        echo $post['Post']['title']; 
    endforeach; 
    

    Then in your view:

    element('posts'); ?>
    

    This is mostly taken from the CakePHP book here: Creating Reusable Elements with requestAction

    I do believe that using requestAction is quite expensive, so you will want to look into caching.

提交回复
热议问题