我是第一次接触 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:我的英文不好,我是学生!!!;)