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.
但它没有用。
7 个解决方案
#1
10
Here is how you can use view helpers from within a controller in 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);
}
#2
9
$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.
在视图中使用此代码即可。但我不知道这是正确的方法。
#3
4
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');
}
#4
2
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);
}
#5
1
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');
#6
1
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');
#7
1
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(); ?>
#1
10
Here is how you can use view helpers from within a controller in 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);
}
#2
9
$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.
在视图中使用此代码即可。但我不知道这是正确的方法。
#3
4
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');
}
#4
2
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);
}
#5
1
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');
#6
1
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');
#7
1
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(); ?>