Thursday, January 16, 2014

Call different layout for different controller action

Change layout in the controller 

public function somethingAction (){

    // statements

   $this->layout('layout/different');

}

How to handle the meta title,description/keywords in a Zend Framework 2 application?

To inject Title, Meta Keywords, Meta Description

Layput file code -

layout.phtml

<html>
<head>
<?php echo $this->headTitle(); ?>

<!-- Display Meta Keywords and Description along with Viewport -->

<?php echo $this->headMeta()->appendName('viewport', 'width=device-width, initial-scale=1.0') ?>

<meta charset="utf-8">

</head>
<body>
...
</body>
</html>

Do following code in the Controller like this:

public function indexAction()

{

 $renderer = $this->getServiceLocator()->get('Zend\View\Renderer\PhpRenderer');

 $renderer->headTitle('Head Title');

 $renderer->headMeta()->appendName('keywords', 'Meta Keywords');

 $renderer->headMeta()->appendName('description', 'Meta Description');

 return new ViewModel();

}