我想不出在 zend 2 中从任何地方生成 Url
我得到了 Action 和 Controller ,所以我试试这个:
$this->url('myControllerName', array('action' => 'myActionName'));
但这会返回一个对象,我只想要这条路线的完整 URL 字符串
有人可以帮我找到正确的方法吗?
编辑:根据 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'
)
)
),
还有我的电话:
echo $this->url('indexqvm', array('action' => 'list-index'));
错误: 可捕获的 fatal error :类 Zend\Mvc\Controller\Plugin\Url 的对象无法转换为字符串
最佳答案
在调用 $this->url(...)
之前使用 echo(见下文),这将显示整个 URL。
<?php echo $this->url('route-name', $urlParams, $urlOptions); ?>
请注意,url()
的第一个参数是在您的 [module]/config/module.config.php
文件中指定的路由名称。
参见 this有关 ZF2 的 URL 查看助手的更多信息。
编辑以回应问题编辑:
以上部分与使用 URL View 助手有关。
如果您想要 Controller 中的 URL,那么您需要 URL Controller 插件。
<?php $url = $this->url()->fromRoute('route-name', $params, $options); ?>
This是对该 Controller 插件的 ZF2 手册的引用。
希望这有帮助:)
斯托扬
关于php - ZF2 - 从路线生成网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15766231/