Reputation: 113
Is my first contact with Zend Framework 2 and I have one question:
How to call two controllers a view?
For example: I have the module "Retarifacao": Retarifacao\Controller\RetarifacaoController; Retarifacao\Model\RetarifacaoTable; Retarifacao\Model\Retarifacao.
And within this module have other controller, table and model: Retarifacao\Controller\CCustosController; Retarifacao\Model\CCustosTable; Retarifacao\Model\CCustos.
Respectively on your namespace, I have the indexAction action called in RetarifacaoController, there I need call the method contained in CCustosTable, being this getFixoLocal() in my indexAction setted RetarifacaoController:
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',
),
),
);
I need call this method contained in 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;
}
In my Retarifacao\view\retarifacao\retarifacao\index.phtml.
P.S.: My english is bad, I am student!!! ;)
Upvotes: 0
Views: 458
Reputation: 62874
Don't think about "pulling" data into your view script. View scripts should be fairly ignorant about the rest of the system. Instead, it's the Controllers' job to get all the data and push (inject) it into the viewmodel so your script can use it to render.
Your controllers have access to any services managed by the ServiceManager, so you you would do something like this:
<?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);
}
}
You could be even cleaner than this (injecting the CCustosTable into the controller instead of using the servicemanager, for instance), but this is the simple version.
Upvotes: 1