Reputation: 21
I'm trying to create a View Helper to "inject" a database value to my layout.phtml. It's result with a simple string but when I call a table gateway it's not result and not load the other html.
//Conversa/src/View/Helper/Conversas.php
namespace Conversa\View\Helper;
use Conversa\Model\ConversaTable;
use Zend\View\Helper\AbstractHelper;
class Conversas extends AbstractHelper
{
protected $sm;
protected $mensagemTable;
protected $conversaTable;
public function __construct($sm)
{
$this->sm = $sm;
}
public function __invoke()
{
$id = $_SESSION['id_utilizador'];
//$conversas = $this->getConversaTable()->conversasUtilizadorAll($id);
//$conversaTable = new ConversaTable();
$c = $this->getConversaTable()->fetchAll(); // <-When I call this, it doesn't work anymore
$output = sprintf("I have seen 'The Jerk' %d time(s).", $this->conversaTable);
return htmlspecialchars($output, ENT_QUOTES, 'UTF-8');
}
public function getConversaTable()
{
if (!$this->conversaTable) {
$sm = $this->getServiceLocator();
$this->conversaTable = $sm->get('Conversa\Model\ConversaTable');
}
return $this->conversaTable;
}
public function getMensagemTable()
{
if (!$this->mensagemTable) {
$sm = $this->getServiceLocator();
$this->mensagemTable = $sm->get('Mensagem\Model\MensagemTable');
}
return $this->mensagemTable;
}
}
Module.php
public function getViewHelperConfig()
{
return array(
'factories' => array(
'conversas' => function ($sm) {
$helper = new View\Helper\Conversas;
return $helper;
}
)
);
}
Upvotes: 1
Views: 46
Reputation: 33148
Not much to go on here since you didn't include any info about what happens (error message?), however, your view helper factory doesn't look right. Your view helper constructor method has a required argument for the Service Manager, so you need to pass that:
public function getViewHelperConfig()
{
return array(
'factories' => array(
'conversas' => function ($sm) {
$helper = new View\Helper\Conversas($sm);
return $helper;
}
)
);
}
Also, since your view helper requires conversaTable
, it might be better to pass that to the view helper instead of the service manager (as the service locator functionality you're relying on was removed in ZF3).
Upvotes: 1