Zend framework 在PHP Zend框架中传递变量
我想我只是工作太久了,太累了。我有一个使用Zend框架的应用程序,其中显示了数据库中的俱乐部列表。然后,我希望用户能够单击该俱乐部,并将该俱乐部的id发布到另一个页面,以显示更多信息 这是俱乐部管理员:Zend framework 在PHP Zend框架中传递变量,zend-framework,Zend Framework,我想我只是工作太久了,太累了。我有一个使用Zend框架的应用程序,其中显示了数据库中的俱乐部列表。然后,我希望用户能够单击该俱乐部,并将该俱乐部的id发布到另一个页面,以显示更多信息 这是俱乐部管理员: class ClubsController extends Zend_Controller_Action { public function init() { } public function indexAction() { $th
class ClubsController extends Zend_Controller_Action
{
public function init()
{
}
public function indexAction()
{
$this->view->assign('title', 'Clubs');
$this->view->headTitle($this->view->title, 'PREPEND');
$clubs = new Application_Model_DbTable_Clubs();
$this->view->clubs = $clubs->fetchAll();
}
}
模型:
class Application_Model_DbTable_Clubs extends Zend_Db_Table_Abstract
{
protected $_name = 'clubs';
public function getClub($id) {
$id = (int) $id;
$row = $this->fetchRow('id = ' . $id);
if (!$row) {
throw new Exception("Count not find row $id");
}
return $row->toArray();
}
}
观点:
<table>
<?php foreach($this->clubs as $clubs) : ?>
<tr>
<td><a href=''><?php echo $this->escape($clubs->club_name);?></a></td>
<td><?php echo $this->escape($clubs->rating);?></td>
</tr>
<?php endforeach; ?>
</table>
我想我只是对zend框架的实现方式感到困惑。您可以将俱乐部ID作为href链接到视图中的URL中,例如
/controllername/club/12
,然后在控制器中通过以下方式获取该信息:
$clubId = (int) $this->_getParam('club', false);
如果没有给定参数,“false”将是默认值。(int)是一种很好的做法,可以确保您返回一个数字(如果是其他非数字字符串,则返回0)。您可以将俱乐部ID作为href链接到视图中的URL中,例如
/controllername/club/12
,然后使用以下命令在控制器中获取该信息:
$clubId = (int) $this->_getParam('club', false);
如果没有给定参数,“false”将是默认值。(int)是一种很好的做法,可以确保返回一个数字(如果是其他非数字字符串,则返回0)。在视图中执行此操作
<?php foreach ($this->clubs as $clubs) : ?>
...
<a href="<?php echo $this->url(array(
'controller' => 'club-description',
'action' => 'index',
'club_id' => $clubs->id
));?>">
...
...
在你看来,你应该这样做
<?php foreach ($this->clubs as $clubs) : ?>
...
<a href="<?php echo $this->url(array(
'controller' => 'club-description',
'action' => 'index',
'club_id' => $clubs->id
));?>">
...
...
例如:
class ClubsController extends Zend_Controller_Action
{
public function init()
{
}
public function indexAction()
{
$this->view->assign('title', 'Clubs');
$this->view->headTitle($this->view->title, 'PREPEND');
$clubs = new Application_Model_DbTable_Clubs();
$this->view->clubs = $clubs->fetchAll();
}
public function displayAction()
{
//get id param from index.phtml (view)
$id = $this->getRequest()->getParam('id');
//get model and query by $id
$clubs = new Application_Model_DbTable_Clubs();
$club = $clubs->getClub($id);
//assign data from model to view [EDIT](display.phtml)
$this->view->club = $club;
//[EDIT]for debugging and to check what is being returned, will output formatted text to display.phtml
Zend_debug::dump($club, 'Club Data');
}
}
[编辑]display.phtml
<!-- This is where the variable passed in your action shows up, $this->view->club = $club in your action equates directly to $this->club in your display.phtml -->
<?php echo $this->club->dataColumn ?>
视图index.phtml
<table>
<?php foreach($this->clubs as $clubs) : ?>
<tr>
<!-- need to pass a full url /controller/action/param/, escape() removed for clarity -->
<!-- this method of passing a url is easy to understand -->
<td><a href='/index/display/id/<?php echo $clubs->id; ?>'><?php echo $clubs->club_name;?></a></td>
<td><?php echo $clubs->rating;?></td>
</tr>
<?php endforeach; ?>
使用url()帮助器的示例视图
[编辑]
根据模型中当前getClub()方法的构建方式,您可能需要使用$club['data']
访问数据。可以通过从返回值中删除->toArray()
来更正此问题。
如果您还没有这样做,您可以通过在.htaccess文件SetEnv应用程序\u ENV development
中添加以下行来激活屏幕上的错误消息。
使用您提供的信息,确保display.phtml
位于application\views\scripts\club description\display.phtml
(我非常确定这是正确的,ZF以一种有趣的方式处理一些驼峰案例名称)示例:
class ClubsController extends Zend_Controller_Action
{
public function init()
{
}
public function indexAction()
{
$this->view->assign('title', 'Clubs');
$this->view->headTitle($this->view->title, 'PREPEND');
$clubs = new Application_Model_DbTable_Clubs();
$this->view->clubs = $clubs->fetchAll();
}
public function displayAction()
{
//get id param from index.phtml (view)
$id = $this->getRequest()->getParam('id');
//get model and query by $id
$clubs = new Application_Model_DbTable_Clubs();
$club = $clubs->getClub($id);
//assign data from model to view [EDIT](display.phtml)
$this->view->club = $club;
//[EDIT]for debugging and to check what is being returned, will output formatted text to display.phtml
Zend_debug::dump($club, 'Club Data');
}
}
[编辑]display.phtml
<!-- This is where the variable passed in your action shows up, $this->view->club = $club in your action equates directly to $this->club in your display.phtml -->
<?php echo $this->club->dataColumn ?>
视图index.phtml
<table>
<?php foreach($this->clubs as $clubs) : ?>
<tr>
<!-- need to pass a full url /controller/action/param/, escape() removed for clarity -->
<!-- this method of passing a url is easy to understand -->
<td><a href='/index/display/id/<?php echo $clubs->id; ?>'><?php echo $clubs->club_name;?></a></td>
<td><?php echo $clubs->rating;?></td>
</tr>
<?php endforeach; ?>
使用url()帮助器的示例视图
[编辑]
根据模型中当前getClub()方法的构建方式,您可能需要使用$club['data']
访问数据。可以通过从返回值中删除->toArray()
来更正此问题。
如果您还没有这样做,您可以通过在.htaccess文件SetEnv应用程序\u ENV development
中添加以下行来激活屏幕上的错误消息。
使用您提供的信息,确保display.phtml
位于application\views\scripts\club description\display.phtml
(我非常确定这是正确的,ZF以一种有趣的方式处理一些驼峰案例名称)好的,我有我的俱乐部视图:我有控制器:但我没有传递变量..我必须这样链接才能访问ClubDecriptionController,但这样我不明白如何传递变量?使用@namesnik答案中显示的Url帮助器。好的,我有我的clubs视图:我有控制器:但我没有传递变量..我必须这样链接才能访问ClubDecriptionController,但是用这种方式我不明白如何传递变量?使用@namesnik答案中显示的Url帮助器。太好了!我在url中有俱乐部id,但是$this->getRequest()->getParam('club_id')到底做什么?当我把它放在ClubDescriptionController的索引中时,我什么也得不到。我是否必须创建一个函数来通过id获取俱乐部信息?这看起来像什么?getParam('club_id')
获取GET varclub_id的值。使用此ID,您可以使用Zend_Db_Table
对象的find()
方法获取俱乐部信息:$clubs=new Application_Model_DbTable_clubs()
然后$row=$clubs->find($club\u id)
好的,这样我就理解了url助手的工作方式,而且它是有效的。但是,我很难理解的是如何从ClubsDescriptionController中的id获取数据。我有这样一个:如果我尝试在控制器的视图中回显俱乐部名称,它不会做任何事情。在您的视图中,请参考$this->clubs,而不仅仅是$clubs。所以它应该是这样的echo$this->escape($this->clubs->club\u name)代码>它仍然不起作用。。此外,如果我将displayAction放在ClubDescriptionController的indexAction中,则会中断应用程序。所以这里也有问题。ClubsDescriptionController很棒!我在url中有俱乐部id,但是$this->getRequest()->getParam('club_id')到底做什么?当我把它放在ClubDescriptionController的索引中时,我什么也得不到。我是否必须创建一个函数来通过id获取俱乐部信息?这看起来像什么?getParam('club_id')
获取GET varclub_id的值。使用此ID,您可以使用Zend_Db_Table
对象的find()
方法获取俱乐部信息:$clubs=new Application_Model_DbTable_clubs()
然后$row=$clubs->find($club\u id)
好的,这样我就理解了url助手的工作方式,而且它是有效的。但是,我很难理解的是如何从ClubsDescriptionController中的id获取数据。我有这样的想法:如果我尝试在控制器的视图中回显俱乐部名称,它不会做任何事情。在您的视图中,指的是$t