php - how to access variables in a different controller with Cakephp -


right, have:

controller/postscontroller.php model/post.php view/posts/index.ctp 

the controller finds posts in database , passes values view/posts/index.ctp, foreach though each post.

i want able access same posts on controller , view. how do this?

postscontroller is;

class postscontroller extends appcontroller {       public function index() {         $this->set('posts', $this->post->find('all'));      }   } 

view/posts/index.ctp is;

 <table>         <tr>             <th>id</th>             <th>title</th>             <th>actions</th>             <th>created</th>         </tr>      <!-- here's loop through our $posts array, printing out post info -->          <?php foreach ($posts $post): ?>         <tr>             <td><?php echo $post['post']['id']; ?></td>             <td>                 <?php                     echo $this->html->link(                         $post['post']['title']                     );                 ?>             </td>               <td>                 <?php echo $post['post']['created']; ?>             </td>         </tr>         <?php endforeach; ?>      </table> 

now in aboutscontroller , abouts/index.ctp want able access same data can show posts on view/abouts/index.ctp

when make view/abouts/index.ctp, have same code view/posts/index.ctp error message;

undefined variable: posts [app\view\abouts\index.ctp, line 12] 

this because not know how make data accessible in abouts/controller.

for global variable , suggest set un appcontroller can access wherever are, in appcontroller :

class appcontroller extends controller {     var $uses = array('post'); // use model        public function beforefilter(){          $this->set('posts', $this->post->find('all'));       } } 

then can posts in view ...


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -