admin管理员组文章数量:1178553
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('.4.2/jquery.min.js');
return new ViewModel();
}
But it is not working.
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.
Share Improve this question edited Apr 27, 2012 at 9:04 vascowhite 18.4k9 gold badges63 silver badges78 bronze badges asked Apr 27, 2012 at 8:00 Mangala EdirisingheMangala Edirisinghe 1,1112 gold badges16 silver badges32 bronze badges 3 |7 Answers
Reset to default 10Here 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);
}
$this->HeadScript()->appendFile('http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js','text/javascript');
$this->HeadScript()->appendFile('http://localhost/zend/public/js/validate_jquary.js','text/javascript');
It is OK with this code in the view. But I don't know is this correct method.
Probably the easiest way to use view helpers from within a controller in ZF2 is via the renderer object:
public function someAction()
{
$renderer = $this->serviceLocator->get('Zend\View\Renderer\RendererInterface');
$renderer->headScript()->appendFile($renderer->baseUrl() . '/js/somejs.js');
}
you can try this. its works fine for me
//write these lines in your SampleController
public function someAction()
{
$this->getViewHelper('HeadScript')->appendFile('/js/yourjsfile.js');
$this->getViewHelper('HeadScript')->appendFile('/js/jquery/jquery.min.js');
}
// write following method in controller
protected function getViewHelper($helperName)
{
return $this->getServiceLocator()->get('viewhelpermanager')->get($helperName);
}
You aren't using the view to add jquery:
$this->view->headScript()->appendFile('http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');
a good way for that is to use the below code in your controller action lets say u want to include the paginator.js
$this->view->headScript()->appendFile($this->view->baseUrl().'/js/paginator.js');
All of the above is giving tons of errors for me and $this->view->headScript() is at all about Zend Framework 1. This works for me:
in your controller before controller's class definition add:
use Zend\View\Helper\HeadScript;
and then you may use something like this in your controller (sure you may use it in any action, not only in the constructor):
/**
* @var Zend\View\Helper\HeadScript
*/
protected $headScript;
function __construct() {
$this->headScript = new HeadScript();
$this->headScript->appendFile('http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js','text/javascript');
}
and then you should add this to your layout:
<?php echo $this->headScript(); ?>
本文标签: jqueryHow to add an external javascript file to a Zend Framework 2 applicationStack Overflow
版权声明:本文标题:jquery - How to add an external javascript file to a Zend Framework 2 application? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738055950a2056265.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$this->headScript()
in your layout or view? Also, the fact that you have a return statement in your action doesn't look right to me, although I'm not familiar with ZF2, so it may be valid. – vascowhite Commented Apr 27, 2012 at 9:05$this->headScript()
in the controller. – Mangala Edirisinghe Commented Apr 30, 2012 at 4:05