ZF2 - 从路由生成URL

时间:2013-04-02 14:06:11

标签: php url action zend-framework2

我无法想象在我想要的任何地方生成Url,在zend 2中

我得到动作和控制器所以我试试这个:

$this->url('myControllerName', array('action' => 'myActionName'));

但是这会返回一个对象,我只想要这条路线的完整URL字符串

有人可以帮我找到合适的方法吗?

编辑:根据斯托扬的说法,也许我在路线上犯了一个错误。这是我的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'));

错误:     可捕获的致命错误:类Zend \ Mvc \ Controller \ Plugin \ Url的对象无法转换为字符串

2 个答案:

答案 0 :(得分:25)

在调用$this->url(...)之前使用echo(参见下文),这将显示整个网址。

<?php echo $this->url('route-name', $urlParams, $urlOptions); ?>

请注意,url()的第一个参数是[module]/config/module.config.php文件中指定的路线名称。

有关ZF2的URL视图助手的详细信息,请参阅this

编辑回复问题编辑:

以上部分与使用URL视图助手有关。

如果您想在控制器中使用URL,则需要URL控制器插件。

<?php $url = $this->url()->fromRoute('route-name', $params, $options); ?>

This是对此控制器插件的ZF2手册的引用。

希望这会有所帮助:)

Stoyan

答案 1 :(得分:1)

您可以在.phtml文件中使用它

echo $this->url('HelloWorld/default', array('controller'=>'Index', 'action'=>'registration'));

HelloWorld / default是路由,其余是控制器及其操作,你也可以发送其他参数,只是在数组中添加为键和值对。

相关问题