php CakePHP 将数据传递给元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5523162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CakePHP passing data to element
提问by Cameron
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.
我希望它做的是 a) 显示索引的投资组合项目列表,例如/portfolio/
和 b) 在元素内显示投资组合项目列表,以便用户可以从我的侧边栏访问整个网站的投资组合项目。
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
谢谢
回答by Bjorn
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:
然后,在您的 index.ctp 视图中:
<?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.
如果您希望能够从站点中的每个页面(侧边栏或其他内容)请求此内容,您可以使用requestAction
,或将$this->set...
放在您的 AppController 中。如果您requestAction
在元素中使用,则不必传入array('posts' => ...)
您的$this->element
调用。
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.
好的,很明显你需要更多的指导。让我一步一步地解释这一点。首先,我们需要beforeFilter
在您的上创建一个AppController
,以便$posts
可以从应用程序的任何地方访问该变量。
/app/app_controller.php
:
/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
:
接下来,我们在 中创建一个简单的元素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.
我们将 $posts(我们已在您的 AppController 中定义)变量分配给items
键,因为我们的元素需要一个$items
变量。
回答by Dunhamzzz
回答by Basant Rules
Element with param
带参数的元素
$this->element('elementname', array('var_name'=>$var));
回答by contrebis
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]
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?
是在控制器中。您发布的内容看起来不错(虽然代码略显冗长),所以我会问 PortfolioController 中的第 16 行在哪里,为什么没有定义 $posts ?
Returning a value from a controller to use in the element works in general so no problem there.
从控制器返回一个值以在元素中使用通常可以正常工作,因此没有问题。
回答by rajesh_kw
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);
}
}
回答by dogmatic69
you are setting the $posts var in the element and trying to pass it from the layout to the element o.o
您正在元素中设置 $posts var 并尝试将其从布局传递给元素 oo
your code is going in this order
您的代码按此顺序运行
render layout
include element passing variable
set var with requestAction
foreach
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)
回答by Mouad Debbar
Try this and let me know if it works or not ..
试试这个,让我知道它是否有效..
I made some changes to your Controller::index() function :
我对您的 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 );
}
}
no changes to your element portfolio-nav
..
你的元素没有变化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..
我希望这对你有帮助..祝你的发展好运..
回答by user3183399
View Cakephp Documentation http://book.cakephp.org/2.0/en/views.html
查看 Cakephp 文档 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.
如果您需要在每个视图中使用该变量,您应该在会话中设置它。
回答by dragolex10
//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')); ?>
回答by OldWest
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 />";
}