Php 将两个控制器调用为view Zend框架
这是我第一次接触Zend Framework 2,我有一个问题: 如何在一个视图中调用两个控制器 例如: 我有“Retarifacao”模块: Retarifaco\Controller\Retarifacocontroller; Retarifacao\Model\retarifacable; Retarifacao\Model\Retarifacao 在该模块内有其他控制器、表格和模型: Retarifacao\Controller\CCustosController; Retarifacao\Model\ccustostatable; Retarifacao\Model\CCustos 分别在您的命名空间上,我在RetarifaCoController中调用了indexAction操作,在那里我需要调用CCustosTable中包含的方法,即我的indexAction设置的RetarifaCoController中的getFixLocal(): module.phpPhp 将两个控制器调用为view Zend框架,php,zend-framework2,Php,Zend Framework2,这是我第一次接触Zend Framework 2,我有一个问题: 如何在一个视图中调用两个控制器 例如: 我有“Retarifacao”模块: Retarifaco\Controller\Retarifacocontroller; Retarifacao\Model\retarifacable; Retarifacao\Model\Retarifacao 在该模块内有其他控制器、表格和模型: Retarifacao\Controller\CCustosController; Retarifaca
<?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);
},
),
);
}
}
在我的retarifaco\view\retarifaco\retarifaco\index.phtml中
我的英语很差,我是学生!!!;) 不要考虑将数据“拉入”到视图脚本中。视图脚本应该对系统的其余部分相当无知。相反,控制器的任务是获取所有数据并将其推(注入)到viewmodel中,以便脚本可以使用它进行渲染
您的控制器可以访问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);
}
}
非常感谢您的解决方案解决了我的问题,timdev!!!生活和学习!!!=)
public function getFixoLocal(){
$rowset = $this->tableGateway->select(array('tipo_fixo' => 'fixo_local'));
$row = $rowset->current();
if($row)
return $row;
else
return false;
}
<?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);
}
}