【问题标题】:Calling two controllers a view Zend Framework调用两个控制器一个视图 Zend Framework
【发布时间】:2013-08-07 19:33:21
【问题描述】:

我是第一次接触 Zend Framework 2,我有一个问题:

如何调用两个控制器一个视图?

例如: 我有模块“Retarifacao”: Retarifacao\Controller\RetarifacaoController; Retarifacao\Model\RetarifacaoTable; Retarifacao\Model\Retarifacao.

在这个模块中还有其他控制器、表和模型: Retarifacao\Controller\CCustosController; Retarifacao\Model\CCustosTable; Retarifacao\Model\CCustos。

分别在您的命名空间中,我在 RetarifacaoController 中调用了 indexAction 操作,我需要调用 CCustosTable 中包含的方法,即在我的 indexAction 设置的 RetarifacaoController 中的 getFixoLocal():

模块.php

<?php 
    namespace Retarifacao;

    use Retarifacao\Model\Retarifacao;
    use Retarifacao\Model\RetarifacaoTable;
    use Retarifacao\Model\CCustos;
    use Retarifacao\Model\CCustosTable;
    use Zend\Db\ResultSet\ResultSet;
    use Zend\Db\TableGateway\TableGateway;

    class Module
    {


    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'Retarifacao\Model\RetarifacaoTable' =>  function($sm) {
                    $tableGateway = $sm->get('RetarifacaoTableGateway');
                    $table = new RetarifacaoTable($tableGateway);
                    return $table;
                },
                'RetarifacaoTableGateway' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Retarifacao());
                    return new TableGateway('vc_tarifas', $dbAdapter, null, $resultSetPrototype);
                },
                'Retarifacao\Model\CCustosTable' =>  function($sm) {
                    $tableGateway = $sm->get('CCustosTableGateway');
                    $table = new CCustosTable($tableGateway);
                    return $table;
                },
                'CCustosTableGateway' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Retarifacao());
                    return new TableGateway('ccustos', $dbAdapter, null, $resultSetPrototype);
                },
            ),
        );
    }
}

module.config.php

<?php
return array(
    'controllers' => array(
        'invokables' => array(

            /**
             * NAMESPACES DA TABELA
             */
            'Retarifacao\Controller\Retarifacao'    => 'Retarifacao\Controller\RetarifacaoController',
            'Retarifacao\Controller\CCustos'        => 'Retarifacao\Controller\CCustosController',
        ),
    ),

    'router' => array(
        'routes' => array(
            'retarifacao' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/retarifacao[/][:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Retarifacao\Controller\Retarifacao',
                        'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),

    'view_manager' => array(
        'template_path_stack' => array(
            'retarifacao' => __DIR__ . '/../view',
        ),
    ),
);

我需要调用 Retarifacao\Model\CCustosTable.php 中包含的这个方法:

public function getFixoLocal(){
    $rowset = $this->tableGateway->select(array('tipo_fixo' => 'fixo_local'));
    $row    = $rowset->current();
    if($row)
        return $row;
    else
        return false;
}

在我的 Retarifacao\view\retarifacao\retarifacao\index.phtml 中。

P.S.:我的英文不好,我是学生!!! ;)

【问题讨论】:

    标签: php zend-framework2


    【解决方案1】:

    不要考虑将数据“拉”到您的视图脚本中。查看脚本应该对系统的其余部分一无所知。相反,控制器的工作是获取所有数据并将其推送(注入)到视图模型中,以便您的脚本可以使用它进行渲染。

    您的控制器可以访问由 ServiceManager 管理的任何服务,因此您可以执行以下操作:

    <?php
    class RetarifacaoController extends AbstractActionController{
    
        public function indexAction(){
    
            // get the CCustosTable service.
            $CCustosTable = $this->getServiceLocator()->get('Retarifacao\Model\CCustosTable');
    
            // get the data from the service.
            $fixoLocalData = $CCustosTable->getFixoLocal();
    
            // implicitly creates a ViewModel to be rendered.  $fixoLocalData is will be available 
            // in your view script.
            return array('fixoLocalData'=>$fixoLocalData);
        }
    }
    

    您甚至可以比这更干净(例如,将 CCustosTable 注入控制器而不是使用 servicemanager),但这是简单的版本。

    【讨论】:

    • 非常感谢您的解决方案解决了我的问题 timdev !!!生活与学习!!! =)
    猜你喜欢
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    • 2012-06-30
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多