Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an external javascript file to a Zend Framework 2 application?

I need to add jQuery and other javascript files to my Zend Framework project. I am trying to do it with an Action controller:-

public function userinfoAction()
{   
    $this->view->headScript()->appendFile($basePath .'/js/validate_jquary.js');
    $this->headScript()->appendFile('http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'); 
    return new ViewModel();
}

But it is not working.

like image 269
Mangala Edirisinghe Avatar asked Apr 27 '12 08:04

Mangala Edirisinghe


1 Answers

Here is how you can use view helpers from within a controller in ZF2 to solve your problem:

public function someAction()
{                 
     $this->getViewHelper('HeadScript')->appendFile($basePath . '/js/somejs.js');    
}

protected function getViewHelper($helperName)
{
    return $this->getServiceLocator()->get('viewhelpermanager')->get($helperName);
}
like image 137
aimfeld Avatar answered Sep 22 '22 15:09

aimfeld