Reputation: 6321
I'm trying to convert our existing application from Zend Framework 1 to Zend Framework 2 and I'm having a little trouble.
In the original application I had the following in my controllers
function init()
{
$this->initialize_values();
}
All my controllers extended a base class which had that function in it as seen here.
protected function initialize_values()
{
$this->_db = Zend_Registry::get('dbAdapter');
$this->_current_user = new User(&$this->_db);
$this->_auth = Zend_Auth::getInstance();
if($this->_auth->hasIdentity())
{
$this->_current_user = unserialize($_SESSION['current_user']);
$this->view->current_user = $this->_current_user;
}
}
I've replicated all the functionality except the last line where I set that view value.
It seems in all the examples I'm finding for ZF2 they are returning an array or a viewmodel. I'm not seeing a way to pass a value to the view not attached to an action function.
Upvotes: 2
Views: 14512
Reputation: 362
I have an Application module that loads the main layout.phtml. I added the following code and it worked for me (ZF 2.2.2).
Application/Module.php in onBootstrap(MvcEvent $e):
$auth = new AuthenticationService();
$authenticated = false;
if ($auth->hasIdentity()) {
$authenticated = true;
}
$viewModel = $e->getViewModel();
$viewModel->setVariable('authenticated', $authenticated);
In layout.phtml:
<?php
if ($this->authenticated === true) { ?>
<li class="active"><a href="<?php echo $this->url('logout') ?>"><?php echo $this->translate('Logout') ?></a></li>
<?php } else { ?>
<li class="active"><a href="<?php echo $this->url('login') ?>"><?php echo $this->translate('Login') ?></a></li>
<li class="active"><a href="<?php echo $this->url('register') ?>"><?php echo $this->translate('Sign Up') ?></a></li>
<?php } ?>
Upvotes: 1
Reputation: 103
All you need to do is to store all your view variables into an array and return the array. You don't require any classes or strategies such as ViewModel or JsonModel unless you require the additional functionalities.
So as long as your Action functions in your controller return arrays, you can pass those values to the view.
like so:
public function indexAction()
{
$current_user = $this->getSession();
return array(
'current_user' => $current_user
);
}
And to really go ZF2, use ServiceManagers and ServiceLocators instead of Zend_Registry. You can assign service managers in the Module.php file by creating a function called getServiceConfig() if it doesn't already exist. It should look a bit like this.
public function getServiceConfig()
{
'factories' => array(
'session' => function ($sm){
return new Session($sm->get('Zend/Db/Adapter/Adapter'));
}
)
}
Which I would also consider adding your db adapter through a database.local.php file in the global config/autoload file with a service_manager.
like so:
<?php
$Params = array(
'database' => 'YOUR DATABASE',
'username' => 'YOUR USERNAME',
'password' => 'YOUR PASSWORD',
'hostname' => 'YOUR HOSTNAME',
);
return array(
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => function ($sm) use ($Params) {
return new Zend\Db\Adapter\Adapter(array(
'driver' => 'pdo',
'dsn' => 'mysql:dbname='.$Params['database'].';host='.$Params['hostname'],
'database' => $Params['database'],
'username' => $Params['username'],
'password' => $Params['password'],
'hostname' => $Params['hostname'],
));
},
After all this, you can simply get your Session class or whatever you're going to use with a simple function that will use the ServiceLocator in the Controller.
protected $session;
public function getSession()
{
if($this->session == null)
{
$sl = $this->getServiceLocator();
$this->session = $sl->get('session');
}
return $this->session;
}
Upvotes: 1
Reputation: 3534
That's because it's up to you to create the new ViewModel()
and return it to the calling script. Depending on what you return from your controller, the appropriate renderer gets invoked.
For example if i'm returning a block of html, i'd use
return new ViewModel(array('results' => $results));
at the end of my controller action. However, if i wanted to deliver JSON output i would return
return new JsonModel($results);
In your case, you could prepare your view model and store it in a protected variable and add variables to it like this:
// in your constructor
$this->_view = new ViewModel();
// in your initialize values method
$this->_view->setVariable($name, $value);
Then when ready to output:
// in your controller action
return $this->_view;
Upvotes: 9