[英]Calling two controllers a view Zend Framework
是我與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():
module.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中。
PS:我的英語不好,我是學生!!! ;)
不要考慮將數據“拉”到視圖腳本中。 View腳本應該對系統的其余部分一無所知。 取而代之的是,控制器的工作是獲取所有數據並將其推送(注入)到視圖模型中,以便您的腳本可以使用它來呈現。
您的控制器有權訪問由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),但這是簡單的版本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.