Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP passing data to element

Tags:

php

cakephp

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;
    }
    else
    {
        $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'); ?>
<ul>
    <?php foreach ($posts as $post): ?>
    <li><?php echo $this->Html->link($post['Portfolio']['title'], array('action' => 'view', $post['Portfolio']['id']));?></li>
    <?php endforeach; ?>
</ul>

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.

Thanks

like image 444
Cameron Avatar asked Apr 02 '11 12:04

Cameron


People also ask

What is element in CakePHP?

An element is basically a mini-view. We can also pass variables in elements. Cake\View\View::element(string $elementPath, array $data, array $options =[] There are three arguments to the above function as follows − The first argument is the name of the template file in the /src/Template/element/ folder.

How can I redirect to another page in cakephp?

You can use: $this->redirect(array("controller" => "myController", "action" => "myAction", "param1" => "val1", "param2" => "val2", $data_can_be_passed_here), $status, $exit); Hope it helps!

How do I create a view in cakephp?

php file at src/Controller/ProductsController. Copy the following code in the controller file. Create a directory Products at src/Template and under that folder create a View file called view. php. Copy the following code in that file.


2 Answers

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.

/app/app_controller.php:

<?php
class AppController extends Controller {
    function beforeFilter() {
        parent::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.

like image 106
Bjorn Avatar answered Oct 16 '22 15:10

Bjorn


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

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

Please read the docs.

like image 39
Dunhamzzz Avatar answered Oct 16 '22 17:10

Dunhamzzz