web-dev-qa-db-ja.com

データを要素に渡すCakePHP

コントローラに次のコードがあります。

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'));
    }
}

そして私がそれをしたいのはa)インデックスのポートフォリオアイテムのリストを表示することです。 /portfolio/およびb)要素内のポートフォリオアイテムのリストを表示して、ユーザーがサイト全体のサイドバーからポートフォリオアイテムにアクセスできるようにします。

これがサイドバーの私の要素です:

<?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>

そして、私はそれを私のレイアウトでそのように呼びます:

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

ただし、次のエラーが発生します。

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

また、サイドバーにアイテムのリストは表示されません。

私がコントローラーに書いたものはゴミだと確信しているので、誰かが私がそれを機能させるのを手伝ってくれるなら、それは素晴らしいことです。

ありがとう

13
Cameron

私は昨日まったく同じ質問に答えました。コントローラーのアクションが非常に複雑なのはなぜですか?私はあなたが何も必要としないと思います

_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... 
}
_

次に、index.ctpビューで:

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

サイトのすべてのページ(サイドバーなど)からこれをリクエストできるようにする場合は、requestActionを使用するか、AppControllerに_$this->set..._を配置します。要素でrequestActionを使用する場合、_$this->element_呼び出しでarray('posts' => ...)を渡す必要はありません。


わかりました。もっと多くの方向性が必要なのは明らかです。これを段階的に説明しましょう。まず、beforeFilterAppControllerを作成する必要があります。これにより、_$posts_変数にアプリケーションのどこからでもアクセスできるようになります。

_/app/app_controller.php_:

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

次に、_app/views/elements/foobar.ctp_に単純な要素を作成します。

_<?php debug($items); ?>
_

最後に、ビューのどこかから要素を呼び出します。

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

要素は_$items_変数を想定しているため、$ posts(AppControllerで定義した)変数にitemsキーを割り当てています。

27
Bjorn

Elementメソッドの2番目の引数は、データを渡します。必要なものは次のとおりです。

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

docs をお読みください。

7
Dunhamzzz

パラメータを持つ要素

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

私が取り上げていないと思うことの1つは、エラーメッセージです。

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

コントローラにあります。あなたが投稿したものは(少し冗長なコードですが)問題ないように見えるので、PortfolioControllerの16行目はどこにあり、$ postsが定義されていないのはなぜですか?

コントローラーから値を返して要素で使用することは一般的に機能するので、問題はありません。

2
contrebis

この方法でも実行できます。

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);
    }
}
1
rajesh_kw

要素に$ posts変数を設定し、それをレイアウトから要素o.oに渡そうとしています。

あなたのコードはこの順序で進んでいます

render layout
include element passing variable
set var with requestAction

foreach

あなたがしている必要があります

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)
1
dogmatic69

これを試して、それが機能するかどうかを知らせてください..

Controller :: index()関数にいくつかの変更を加えました:

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 );
    }
}

要素に変更はありませんportfolio-nav

レイアウトの呼び出しを次のように変更します。

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

これがあなたのお役に立てば幸いです。あなたの開発に頑張ってください。

1
Mouad Debbar
//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
dragolex10

これは私がこれを非常に簡単に解決した方法です:

コントローラー:

function birdsTwo() { 
    return $this->Bird->find('all', array('order' => 'Bird.name 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 />";
}
0
OldWest

Cakephpのドキュメントを見る http://book.cakephp.org/2.0/en/views.html

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

すべてのビューで変数が必要な場合は、セッションで設定する必要があります。

0
user3183399