在PHP Zend Framework中传递变量

时间:2012-04-11 13:28:11

标签: zend-framework

我想我刚刚工作太久而且累了。我有一个使用Zend Framework的应用程序,我从数据库中显示一个俱乐部列表。然后我希望用户能够点击俱乐部并将俱乐部的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();
    }
}

模特:

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框架感到困惑。

3 个答案:

答案 0 :(得分:2)

在您的视图中

执行此操作

<?php foreach ($this->clubs as $clubs) : ?>
...
<a href="<?php echo $this->url(array(
                    'controller' => 'club-description',
                    'action' => 'index',
                    'club_id' => $clubs->id
                    ));?>">
...

这样,您就可以在ClubDescription控制器的索引操作中使用club_id参数。你得到这个$this->getRequest()->getParam('club_id')

答案 1 :(得分:1)

一个例子:

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()帮助程序

的示例视图
<table>
        <?php foreach($this->clubs as $clubs) : ?>
        <tr>
        <!-- need to pass a full url /controller/action/param/, escape() removed for clarity -->
        <!-- The url helper is more correct and less likely to break as the application changes -->
            <td><a href='<?php echo $this->url(array(
                'controller' => 'index',
                'action' => 'display',
                'id' => $clubs->id
             )); ?>'><?php echo $clubs->club_name;?></a></td>
            <td><?php echo $clubs->rating;?></td>
        </tr>
    <?php endforeach; ?>
</table>

[编辑] 通过构建模型中当前的getClub()方法,您可能需要使用$club['data']访问数据。可以通过从返回值中删除->toArray()来更正此问题 如果您还没有完成,那么您可以通过在.htaccess文件SetEnv APPLICATION_ENV development中添加以下行来激活屏幕上的错误消息。 使用您提供的信息,确保display.phtml位于application\views\scripts\club-description\display.phtml(我很确定这是正确的,ZF以有趣的方式处理一些驼峰案名称)

答案 2 :(得分:0)

您可以将俱乐部ID放入您链接到的URL中作为视图中的href - 例如/controllername/club/12,然后使用以下命令在控制器中获取该信息:

$clubId = (int) $this->_getParam('club', false);

如果没有给出参数,'false'将是默认值。 (int)是一个很好的做法,以确保你得到一个数字(或0,如果它是一些其他非数字字符串)。