我需要将jQuery和其他javascript文件添加到我的Zend Framework项目中。我正试着用一个动作控制器来实现它:
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();
}但它不起作用。
发布于 2012-04-30 12:11:26
$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');在视图中使用此代码就可以了。但我不知道这是正确的方法。
发布于 2012-11-30 18:14:26
下面是如何从ZF2中的控制器中使用视图帮助器来解决问题:
public function someAction()
{
$this->getViewHelper('HeadScript')->appendFile($basePath . '/js/somejs.js');
}
protected function getViewHelper($helperName)
{
return $this->getServiceLocator()->get('viewhelpermanager')->get($helperName);
}发布于 2012-11-30 19:51:05
从ZF2中的控制器中使用视图辅助对象的最简单方法可能是通过渲染器对象:
public function someAction()
{
$renderer = $this->serviceLocator->get('Zend\View\Renderer\RendererInterface');
$renderer->headScript()->appendFile($renderer->baseUrl() . '/js/somejs.js');
}https://stackoverflow.com/questions/10346952
复制相似问题