0

我是第一次接触 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);
                },
            ),
        );
    }
}

模块.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 中。

PS:我的英文不好,我是学生!!!;)

4

1 回答 1

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),但这是简单的版本。

于 2013-08-07T21:17:36.437 回答