php - ZF2 - 从路线生成网址

标签 php url action zend-framework2

我想不出在 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/

相关文章:

c# - 如何从图片url中解析出图片名称

swift - 是否可以以编程方式禁用 commitEditingStyle?

php - 如何在PHP中添加css文件?

尝试使用 UTF-8 字符串时 PHP htmlspecialchars() 函数出错

Python script.py 文件 url 参数

java.lang.IllegalArgumentException : Illegal character in path at index 33: https://box. one.th/app/api/上传

silverlight - 一个 View 模型用于多个页面

android - 在不打开 Android 应用程序的情况下执行操作

PHP 根据对应的计数数组创建多维数组

php - 如果找不到第二个表行,如何显示第一行