Php 从控制器使用对象Zend\View\Helper\Url
对不起,我英语不好,我来自俄罗斯 开始学习zend框架2。。。 控制器中的示例,readAction(): 因此,引发异常:Php 从控制器使用对象Zend\View\Helper\Url,php,zend-framework2,Php,Zend Framework2,对不起,我英语不好,我来自俄罗斯 开始学习zend框架2。。。 控制器中的示例,readAction(): 因此,引发异常: Zend\View\Exception\RuntimeException File: W:\home\zf2\vendor\zendframework\ zendframework\library\Zend\View\Helper\Url.php:80 Message: No RouteStackInterface instance pro
Zend\View\Exception\RuntimeException
File:
W:\home\zf2\vendor\zendframework\
zendframework\library\Zend\View\Helper\Url.php:80
Message:
No RouteStackInterface instance provided
请帮帮我。
我做错了什么?您不能在控制器中使用viewhelper,也不需要。 还有一个
Url
controller插件,其功能与此基本相同
控制器插件是可调用的类,您可以这样使用它们(控制器的操作上下文):
所有参数都是可选的。有关更多信息,请查看
Zend\Mvc\Controller\Plugin\Url中的代码,或者阅读帮助程序和插件,虽然它们可以直接实例化,但通常不应该,因为它们通常由PluginManager管理。PluginManager是ServiceManager的一个子类,因此,它提供了许多关于插件创建的功能,包括注入标准协作者、使用工厂来提供依赖注入等等
在Url帮助器的例子中,有一个工厂可以确保它已经注入了配置好的路由器——如果您直接实例化它,您将不会有这个,并且它将无法完成它的工作,正如您所注意到的
正如Daniel指出的,您还需要确保获得适合上下文的插件。如果你在一个控制器中,检查是否有一个控制器插件可以帮你完成这项工作。Daniel链接到了他帖子中的文档。我知道已经有了一个公认的答案,但我仍然会发布这个帖子,以减轻像我这样的初学者的负担
我使用smarty作为视图渲染器,在smarty中,您不能在视图中使用php代码,因此,一切都必须在控制器中完成,在变量中分配它们,然后将它们传递给视图
您可以使用以下选项:
$url = $this->url()->fromRoute('route',array('name'=>'route-name'));
如果您正在学习zend 2的教程,它将是这样的:
$url = $this->url()->fromRoute('album',array('action'=>'add'));
$url = $this->url()->fromRoute('album',array('action'=>'edit'));
$url = $this->url()->fromRoute('album',array('action'=>'delete'));
$url = $uri = $this->getRequest()->getUri();
$url = sprintf('%s://%s', $uri->getScheme(), $uri->getHost());
$url .= $this->url()->fromRoute('album',array('action'=>'add'));
其值为:
/zf2/index.php/album/add
/zf2/index.php/album/edit
/zf2/index.php/album/delete
如您所见,您需要向其中添加服务器名称,在生成路由url之前,您可以使用以下方法:
$url = $uri = $this->getRequest()->getUri();
$url = sprintf('%s://%s', $uri->getScheme(), $uri->getHost());
总体而言,代码段应如下所示:
$url = $this->url()->fromRoute('album',array('action'=>'add'));
$url = $this->url()->fromRoute('album',array('action'=>'edit'));
$url = $this->url()->fromRoute('album',array('action'=>'delete'));
$url = $uri = $this->getRequest()->getUri();
$url = sprintf('%s://%s', $uri->getScheme(), $uri->getHost());
$url .= $this->url()->fromRoute('album',array('action'=>'add'));
制作:
http://yourservername/zf2/index.php/album/add
希望这对初学者zf2用户有所帮助不是真的。您可以在控制器中使用视图帮助器,但ofc不是很好的主意:$uVH=$this->getServiceLocator()->get('viewhelpermanager')->get('url')$routeUrl=$uvh('route-name',array('param1'=>'param1');etcu让我免于头痛:-)