php ZF2 - 从路线生成网址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15766231/
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
ZF2 - Generating Url from route
提问by Reign.85
I can't figure out to generate Url from everywhere i want to, in zend 2
在 Zend 2 中,我无法想出从我想要的任何地方生成 Url
I get action and controller so i try this:
我得到动作和控制器,所以我试试这个:
$this->url('myControllerName', array('action' => 'myActionName'));
But this return an object, i just want the full URL string of this route
但这返回一个对象,我只想要这条路线的完整 URL 字符串
Somebody can help me to find the proper way?
有人可以帮我找到正确的方法吗?
EDIT : according to Stoyan, maybe i made a mistake on my route. here is the part of my module.config
编辑:根据 Stoyan 的说法,也许我在路线上犯了一个错误。这是我的 module.config 的一部分
'router' => array (
'routes' => array (
'indexqvm' => array (
'type' => 'segment',
'options' => array (
'route' => '/Indexqvm[/:action][/:id_event]',
'constraints' => array (
'action' => '[a-zA-Z][a-zA-Z0-9_-]+',
'id_event' => '[0-9]+'
),
'defaults' => array (
'controller' => 'Qvm\Controller\Indexqvm',
'action' => 'index'
)
)
),
And my call :
我的电话:
echo $this->url('indexqvm', array('action' => 'list-index'));
the error : Catchable fatal error: Object of class Zend\Mvc\Controller\Plugin\Url could not be converted to string
错误:可捕获的致命错误:类 Zend\Mvc\Controller\Plugin\Url 的对象无法转换为字符串
回答by Stoyan Dimov
Use the echo before calling $this->url(...)
(see bellow) and this will display the whole URL.
在调用之前使用 echo $this->url(...)
(见下文),这将显示整个 URL。
<?php echo $this->url('route-name', $urlParams, $urlOptions); ?>
Note that the first paramter of url()
is the name of the route as specified in your [module]/config/module.config.php
file.
请注意,第一个参数url()
是[module]/config/module.config.php
文件中指定的路由名称。
See thisfor more information about ZF2's URL view helper.
请参阅此有关ZF2的URL视图助手的更多信息。
EDIT in response to the question edit:
编辑回应问题编辑:
The above section is related to using the URL view helper.
上一节与使用 URL 视图助手有关。
If you want a URL in the controller then you need the URL controller plugin.
如果您想要控制器中的 URL,那么您需要 URL 控制器插件。
<?php $url = $this->url()->fromRoute('route-name', $params, $options); ?>
Thisis the reference to the ZF2 manual for this controller plugin.
这是此控制器插件的 ZF2 手册的参考。
Hope this helps :)
希望这可以帮助 :)
Stoyan
斯托扬
回答by Azhar Ahmad
You can use this in the .phtml file
您可以在 .phtml 文件中使用它
echo $this->url('HelloWorld/default', array('controller'=>'Index', 'action'=>'registration'));
Where HelloWorld/default is the routing and the remaining is the controller and its action and also you can send the others parameter adding just in array as key and value pair.
其中 HelloWorld/default 是路由,其余是控制器及其操作,您也可以将其他参数作为键值对添加到数组中。