CakePHP passing data to element

Posted on

CakePHP passing data to element – Here in this article, we will share some of the most common and frequently asked about PHP problem in programming with detailed answers and code samples. There’s nothing quite so frustrating as being faced with PHP errors and being unable to figure out what is preventing your website from functioning as it should like php and cakephp . If you have an existing PHP-based website or application that is experiencing performance issues, let’s get thinking about CakePHP passing data to element.

I have the following code in my controller:

function index()
    $posts = $this->set('posts', $this->Portfolio->find('all'));

    if (isset($this->params['requested']))
        return $posts;
        $this->set('posts', $this->Portfolio->find('all'));

and what I want it to do is a) show a list of portfolio items for the index e.g. /portfolio/ and b) show a list of portfolio items inside an element so a user can access the portfolio items from my sidebar across the site.

Here is my element for the sidebar:

<?php $posts = $this->requestAction('portfolio/index'); ?>
    <?php foreach ($posts as $post): ?>
    <li><?php echo $this->Html->link($post['Portfolio']['title'], array('action' => 'view', $post['Portfolio']['id']));?></li>
    <?php endforeach; ?>

and then I call it like so in my layout:

<?php $this->element('portfolio-nav', array('posts' => $posts) ); ?>

However it gives the following error:

Notice (8): Undefined variable: posts [APP/controllers/portfolio_controller.php, line 16]

And doesn’t show the list of items in the sidebar.

I’m pretty sure what I have written in my controller is garbage, so if anyone can help me get it working, that’d be awesome.


Solution :

I answered the very same question yesterday. Why is your controller action so complex? I guess you don’t need anything more than

function index() {
    $this->set('posts', $this->Portfolio->find('all'));
    // Make sure the model Portfolio is accessible from here, i.e. place this
    // action in PortfoliosController or load it using 
    // ClassRegistry::init('Portfolio')->find... 

Then, in your index.ctp view:

<?php echo $this->element('foobar', array('posts' => $posts)); ?>

If you want to be able to request this from every page in your site (sidebar or something), you can use requestAction, or place the $this->set... in your AppController. If you use requestAction in your element, you don’t have to pass array('posts' => ...) in your $this->element call.

Ok, It’s clear you need much more direction. Let me explain this step by step. First we need to create a beforeFilter on your AppController, so the $posts variable is accessible from everywhere in your application.


class AppController extends Controller {
    function beforeFilter() {
        $this->set('posts', ClassRegistry::init('Portfolio')->find('all'));

Next, we’re creating a simple element in app/views/elements/foobar.ctp:

<?php debug($items); ?>

Lastly, we call the element from somewhere in a view:

<?php echo $this->element('foobar', array('items' => $posts)); ?>

We are assigning the $posts (which we have defined in your AppController) variable the items key, because our element expects a $items variable.

The second argument in the element method passes data. What you need is:

$this->element('portfolio-nav', array('posts' => $posts) );

Please read the docs.

Element with param

$this->element('elementname', array('var_name'=>$var));

One thing I don’t think has been picked up on, your error message:

Notice (8): Undefined variable: posts [APP/controllers/portfolio_controller.php, line 16]

is in the controller. What you have posted looks okay (though slightly verbose code), so I would be asking where is line 16 in PortfolioController and why is $posts not defined?

Returning a value from a controller to use in the element works in general so no problem there.

you are setting the $posts var in the element and trying to pass it from the layout to the element o.o

your code is going in this order

render layout
include element passing variable
set var with requestAction


you should be doing

render layout
include element (dont pass anything)
set var with requesAction (you get the data here so why are you trying to pass it in)

Try this and let me know if it works or not ..

I made some changes to your Controller::index() function :

function index () {
    // this is the problem
    // $posts = $this->set('posts', $this->Portfolio->find('all'));

    $posts = $this->Portfolio->find ( 'all' );

    if (isset($this->params['requested']))
        return $posts;
        // you already have the posts
        //$this->set('posts', $this->Portfolio->find('all'));

        $this->set ( 'posts', $posts );

no changes to your element portfolio-nav..

change the call in your layout to become :

<?php $this->element ( 'portfolio-nav' ); ?>

I hope this was helpful for you..
good luck with your development..

You can do it this way as well.

class AppController extends Controller {

    public $var;    

    public function beforeFilter()
         $this->var = ClassRegistry::init('Portfolio')->find('all');
class YourController extends AppController {

    public function index() {


This is how I solved this very simply:


function birdsTwo() { 
    return $this->Bird->find('all', array('order' => ' ASC', 'limit' => 2));   


<?php echo $this->element('quick_5_birds'); ?>


$birds = $this->requestAction('/birds/birdsTwo/');
    foreach($birds as $bird) {
          echo $this->Html->link(__($bird['Bird']['name'], true), 
array('controller' =>'bird','action' => 'view', $bird['Bird']['id'])) . "<br />";

View Cakephp Documentation

echo $this->element('helpbox', array(
    "helptext" => "Oh, this text is very helpful."

If you need the variable in every view, you should set it in the session.

//Controller users
public function getUsers(){

     $users=  $this->User->find("all");

     return $users;

//Element name users


//vista name view
 print_r( $this->element('users')); ?>

Leave a Reply

Your email address will not be published. Required fields are marked *