php Yii app()->createUrl 用于视图操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23327850/
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
Yii app()->createUrl for view action
提问by JalalJaberi
I use generated actions by Gii in a module said News. I have normal view action that works with an id parameter (such as example.com/news/view/id/1
).
When I use this line of code:
我在名为 News 的模块中使用 Gii 生成的操作。我有使用 id 参数(例如example.com/news/view/id/1
)的普通视图操作。
当我使用这行代码时:
Yii::app()->createUrl("news/view",array("id"=>$data->primaryKey))
It generates example.com/news/1
(if $data->primaryKey
is 1). It is not correct.
When I use this line of code:
它生成example.com/news/1
(如果$data->primaryKey
是 1)。这是不正确的。
当我使用这行代码时:
Yii::app()->createUrl("news/view/id/",array("id"=>$data->primaryKey))
It generates example.com/news/id/id/1
(if $data->primaryKey
is 1).
它生成example.com/news/id/id/1
(如果$data->primaryKey
是 1)。
I am so confused! in first situation, this function doesn't generate id as a parameter name, and in second situation, it does! but after manually added id.
What shoud I do to make correct url format with this function?
我感到很困惑!在第一种情况下,此函数不会生成 id 作为参数名称,而在第二种情况下,它会生成!但是在手动添加id之后。
我应该怎么做才能使用此功能制作正确的 url 格式?
Edit:news is a module. I changed the line of code as:
编辑:新闻是一个模块。我将代码行更改为:
Yii::app()->createUrl("news/default/view/id/",array("id"=>$data->primaryKey))
It generates example.com/news/default/view/id/1
that is correct, but I don't want that default!
它生成的example.com/news/default/view/id/1
是正确的,但我不想要那个默认值!
回答by TotPeRo
In config file you have something like this:
在配置文件中你有这样的东西:
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
this create how to look the URL.
这将创建如何查看 URL。
You not need to write the id
parameter when you create URL because is default. Look on the urlmanager rules:
id
创建 URL 时不需要写入参数,因为它是默认值。查看 urlmanager 规则:
Yii::app()->createUrl("news/view/",array("id"=>$data->primaryKey)) => example.com/news/id/1
On module defaut:
在模块默认上:
Yii::app()->createUrl('/news/default/view', array('id' => $data->primaryKey))
You need to create the urlmanager rule... how you want to look at your URL. More details here.
您需要创建 urlmanager 规则...您希望如何查看您的 URL。更多细节在这里。
回答by George Sovetov
Use
用
'rules'=>array(
'news/<controller:\w+>/<id:\d+>'=>'news/<controller>/view',
'news/<controller:\w+>/<action:\w+>/<id:\d+>'=>'news/<controller>/<action>',
'news/<controller:\w+>/<action:\w+>'=>'news/<controller>/<action>',
),
if you have a module names news
.
如果你有一个模块名称news
。
You can try to replace news
with regex but you will face problems with urls matched by several regexes if your regex is too broad. Use something like <module:news|accounting|people>
in rules array key and <module>
in rules array value.
您可以尝试news
用 regex替换,但如果您的 regex 太宽泛,您将面临多个 regex 匹配的 url 的问题。<module:news|accounting|people>
在规则数组键和<module>
规则数组值中使用类似的东西。
If you need more sophisticated url management, or if you cannot solve your task with regexes, you can always extend CUrlManager.
如果您需要更复杂的 url 管理,或者如果您无法使用正则表达式解决您的任务,您可以随时扩展 CUrlManager。
回答by f_anto
trying to check it on this directory: protected/config/main.php sir.
试图在这个目录上检查它:protected/config/main.php 先生。
urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controll<>er>/<action>',
),
),
Please notice is that routed well? :)
请注意,路由是否良好?:)