Skip to main content
superior method
Source Link
Emery King
  • 3.5k
  • 28
  • 35

In your controller action:

$this ->getServiceLocator() ->get('viewhelpermanager') ->get('HeadScript') ->appendFile('/js/custom.js') ; 

You could make this easier by creating an "invokable" "service locator aware" controller plugin.

You can do that in your modules config file:

... 'controller_plugins' => array( 'invokables' => array( 'Head' => 'Application\Controller\Plugin\Head', ) ), ... 

Creating the "Head" class in module/Application/src/Application/Controller/Plugin/Head.php that implements ServiceLocatorAwareInterface and build some methods like javaScript() or styleSheet for example that simply grab the view helper and return it:

return $this ->getServiceLocator() ->getServiceLocator() // Main service Locator ->get('viewhelpermanager') ->get('HeadScript') ; 

Then in your controller it's more like:

$this->Head()->javaScript()->appendFile('/js/custom.js'); 

You could get as fancy as you wanted about it though. Maybe even Magento style with the XML governing the layout per action.

UPDATE

Output is buffered if you're using the ZF2 MVC. So i've found that adding scripts and stylesheets to a page is best done from the template file attached to your view model:

$this->headScript()->appendFile('/js/custom.js', 'text/javascript');

$this->headLink()->appendStylesheet('/css/custom.css');

Keep it out of the controller since it is view related.

In your controller action:

$this ->getServiceLocator() ->get('viewhelpermanager') ->get('HeadScript') ->appendFile('/js/custom.js') ; 

You could make this easier by creating an "invokable" "service locator aware" controller plugin.

You can do that in your modules config file:

... 'controller_plugins' => array( 'invokables' => array( 'Head' => 'Application\Controller\Plugin\Head', ) ), ... 

Creating the "Head" class in module/Application/src/Application/Controller/Plugin/Head.php that implements ServiceLocatorAwareInterface and build some methods like javaScript() or styleSheet for example that simply grab the view helper and return it:

return $this ->getServiceLocator() ->getServiceLocator() // Main service Locator ->get('viewhelpermanager') ->get('HeadScript') ; 

Then in your controller it's more like:

$this->Head()->javaScript()->appendFile('/js/custom.js'); 

You could get as fancy as you wanted about it though. Maybe even Magento style with the XML governing the layout per action.

In your controller action:

$this ->getServiceLocator() ->get('viewhelpermanager') ->get('HeadScript') ->appendFile('/js/custom.js') ; 

You could make this easier by creating an "invokable" "service locator aware" controller plugin.

You can do that in your modules config file:

... 'controller_plugins' => array( 'invokables' => array( 'Head' => 'Application\Controller\Plugin\Head', ) ), ... 

Creating the "Head" class in module/Application/src/Application/Controller/Plugin/Head.php that implements ServiceLocatorAwareInterface and build some methods like javaScript() or styleSheet for example that simply grab the view helper and return it:

return $this ->getServiceLocator() ->getServiceLocator() // Main service Locator ->get('viewhelpermanager') ->get('HeadScript') ; 

Then in your controller it's more like:

$this->Head()->javaScript()->appendFile('/js/custom.js'); 

You could get as fancy as you wanted about it though. Maybe even Magento style with the XML governing the layout per action.

UPDATE

Output is buffered if you're using the ZF2 MVC. So i've found that adding scripts and stylesheets to a page is best done from the template file attached to your view model:

$this->headScript()->appendFile('/js/custom.js', 'text/javascript');

$this->headLink()->appendStylesheet('/css/custom.css');

Keep it out of the controller since it is view related.

added 96 characters in body
Source Link
Emery King
  • 3.5k
  • 28
  • 35

In your controller action:

$this ->getServiceLocator() ->get('viewhelpermanager') ->get('HeadScript') ->appendFile('/js/custom.js') ; 

You could make this easier by creating an "invokable" "service locator aware" controller plugin.

You can do that in your modules config file:

... 'controller_plugins' => array( 'invokables' => array( 'Head' => 'Application\Controller\Plugin\Head', ) ), ... 

Creating the "Head" class in module/Application/src/Application/Controller/Plugin/Head.php that implements ServiceLocatorAwareInterface and build some methods like javaScript() or styleSheet for example that simply grab the view helper and return it:

return $this ->getServiceLocator() ->getServiceLocator() // Main service Locator ->get('viewhelpermanager') ->get('HeadScript')  ; 

Then in your controller it's more like:

$this->Head()->javaScript()->appendFile('/js/custom.js'); 

You could get as fancy as you wanted about it though. Maybe even Magento style with the XML governing the layout per action.

In your controller action:

$this ->getServiceLocator() ->get('viewhelpermanager') ->get('HeadScript') ->appendFile('/js/custom.js') ; 

You could make this easier by creating an "invokable" "service locator aware" controller plugin.

You can do that in your modules config file:

... 'controller_plugins' => array( 'invokables' => array( 'Head' => 'Application\Controller\Plugin\Head', ) ), ... 

Creating the "Head" class in module/Application/src/Application/Controller/Plugin/Head.php that implements ServiceLocatorAwareInterface and build some methods like javaScript() or styleSheet for example that simply grab the view helper and return it:

return $this->getServiceLocator()->getServiceLocator()->get('HeadScript'); 

Then in your controller it's more like:

$this->Head()->javaScript()->appendFile('/js/custom.js'); 

You could get as fancy as you wanted about it though. Maybe even Magento style with the XML governing the layout per action.

In your controller action:

$this ->getServiceLocator() ->get('viewhelpermanager') ->get('HeadScript') ->appendFile('/js/custom.js') ; 

You could make this easier by creating an "invokable" "service locator aware" controller plugin.

You can do that in your modules config file:

... 'controller_plugins' => array( 'invokables' => array( 'Head' => 'Application\Controller\Plugin\Head', ) ), ... 

Creating the "Head" class in module/Application/src/Application/Controller/Plugin/Head.php that implements ServiceLocatorAwareInterface and build some methods like javaScript() or styleSheet for example that simply grab the view helper and return it:

return $this ->getServiceLocator() ->getServiceLocator() // Main service Locator ->get('viewhelpermanager') ->get('HeadScript')  ; 

Then in your controller it's more like:

$this->Head()->javaScript()->appendFile('/js/custom.js'); 

You could get as fancy as you wanted about it though. Maybe even Magento style with the XML governing the layout per action.

Source Link
Emery King
  • 3.5k
  • 28
  • 35

In your controller action:

$this ->getServiceLocator() ->get('viewhelpermanager') ->get('HeadScript') ->appendFile('/js/custom.js') ; 

You could make this easier by creating an "invokable" "service locator aware" controller plugin.

You can do that in your modules config file:

... 'controller_plugins' => array( 'invokables' => array( 'Head' => 'Application\Controller\Plugin\Head', ) ), ... 

Creating the "Head" class in module/Application/src/Application/Controller/Plugin/Head.php that implements ServiceLocatorAwareInterface and build some methods like javaScript() or styleSheet for example that simply grab the view helper and return it:

return $this->getServiceLocator()->getServiceLocator()->get('HeadScript'); 

Then in your controller it's more like:

$this->Head()->javaScript()->appendFile('/js/custom.js'); 

You could get as fancy as you wanted about it though. Maybe even Magento style with the XML governing the layout per action.