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!!! ;)