开发者

CakePHP passing data to element

开发者 https://www.devze.com 2023-02-21 06:35 出处:网络
I have the following code in my controller: function index() { $posts = $this->set(\'posts\', $this->Portfolio->find(\'all\'));

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 variab开发者_开发知识库le: 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


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.


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

foreach

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;
    }
    else
    {
        // 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() {

        debug($this->var);
    }
}


This is how I solved this very simply:

controller:

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

layout:

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

element:

$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 http://book.cakephp.org/2.0/en/views.html

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

$this->requestAction(array('controller'=>'users','action'=>'getUsers'))

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

精彩评论

暂无评论...
验证码 换一张
取 消