ZendFramework 2 - how do you handle from controller the layout head body footers like in ZF1?

3.2k Views Asked by At

How to do the following __construct section shown in ZF1 on the fly in ZF2 way?

I have tried $this->headTitle('..'); by ommiting ->view call, but it still fail by throwing:

Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for headTitle

  public function __construct() { //init is gone 

    $this->_helper->layout()->setLayout('brand');
    $this->HeadTitle($this->title)->setIndent(8);
    $this->view->headMeta()->appendName('keywords', $this->keyword)->setIndent(8);
    $this->view->headMeta()->appendName('description', $this->description)->setIndent(8);
    $this->view->headMeta()->appendName('Language', 'en')->setIndent(8);

    $this->view->headMeta()->appendName('dc.title', $this->title)->setIndent(8);
    $this->view->headMeta()->appendName('dc.keywords', $this->keyword)->setIndent(8);
    $this->view->headMeta()->appendName('dc.description', $this->description)->setIndent(8);

    $this->view->headLink()->appendStylesheet('/css/main.css')->setIndent(8);
    $this->view->headLink()->appendStylesheet('/jquery/css/custom-theme/jquery-ui-
1.8.20.custom.css')->setIndent(8);

    $post = $this->getRequest()->getPost();
    $get = $this->getRequest()->getQuery();
  }
4

There are 4 best solutions below

2
On BEST ANSWER

You could access to 'renderer' object in your action controller:

public function indexAction()
{
    $renderer = $this->getServiceLocator()->get('Zend\View\Renderer\PhpRenderer');
    $renderer->headTitle('My title');

    return new ViewModel();
}
0
On

Write a function to handle it for all actions in a controller

protected function setHeadTitle($title = ''){
    if(!empty($title)){            
        $renderer = $this->getServiceLocator()->get('Zend\View\Renderer\PhpRenderer');
        $renderer->headTitle($title);
    }
}

Use the function in your action

public function loginAction()
{
    $this->setHeadTitle("Login");
    //write some other codes
}
0
On

Write a plugin for all module

class HeadTitlePlugin extends AbstractPlugin
{

    public function setHeadTitle($title = '')
    {
        if (! empty($title)) {
            $renderer = $this->getController()->getServiceLocator()->
                              get('Zend\View\Renderer\PhpRenderer');
            $renderer->headTitle($title);
        }
    }
}

Attach the plugin in module config

'controller_plugins' => array(
    'invokables' => array(
        'HeadTitlePlugin' => 'Modulename\Controller\Plugin\HeadTitlePlugin'
    )
),

Call the plugin function in controller action

public function indexAction()
{
    $this->HeadTitlePlugin()->setHeadTitle("Signup");
    // other codes 
}

Thats all

0
On

I got the same question and I have developed an ZF2 plugin to use headTitle like in layout.phtml file. https://github.com/remithomas/rt-headtitle

public function indexAction(){
  $this->headTitle("My website")->setSeparator(" - ")->append("easy ?!");
  return new ViewModel();
}