如何将外部 javascript 文件添加到 Zend Framework 2 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10346952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add an external javascript file to a Zend Framework 2 application?
提问by Mangala Edirisinghe
I need to add jQuery and other javascript files to my Zend Framework project. I am trying to do it with an Action controller:-
我需要将 jQuery 和其他 javascript 文件添加到我的 Zend Framework 项目中。我正在尝试使用 Action 控制器来做到这一点:-
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.
但它不起作用。
采纳答案by Mangala Edirisinghe
$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.
在视图中使用此代码是可以的。但我不知道这是正确的方法。
回答by aimfeld
Here is how you can use view helpers from within a controllerin ZF2 to solve your problem:
以下是如何在 ZF2 中的控制器中使用视图助手来解决您的问题:
public function someAction()
{
$this->getViewHelper('HeadScript')->appendFile($basePath . '/js/somejs.js');
}
protected function getViewHelper($helperName)
{
return $this->getServiceLocator()->get('viewhelpermanager')->get($helperName);
}
回答by aimfeld
Probably the easiest way to use view helpers from within a controller in ZF2 is via the renderer object:
在 ZF2 中的控制器中使用视图助手的最简单方法可能是通过渲染器对象:
public function someAction()
{
$renderer = $this->serviceLocator->get('Zend\View\Renderer\RendererInterface');
$renderer->headScript()->appendFile($renderer->baseUrl() . '/js/somejs.js');
}
回答by syamasundar chettu
you can try this. its works fine for me
你可以试试这个。它对我来说很好用
//write these lines in your SampleController
//在您的 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);
}
回答by Fawad Ghafoor
a good way for that is to use the below code in your controller action lets say u want to include the paginator.js
一个好方法是在您的控制器操作中使用以下代码,假设您想包含 paginator.js
$this->view->headScript()->appendFile($this->view->baseUrl().'/js/paginator.js');
回答by trushkevich
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:
以上所有内容都给我带来了大量错误,而 $this->view->headScript() 完全是关于 Zend Framework 1。这对我有用:
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(); ?>
回答by Ashley
You aren't using the view to add jquery:
您没有使用视图添加 jquery:
$this->view->headScript()->appendFile('http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');