Yii 基本 url 重写 [英] Yii basic url rewrite

查看:37
本文介绍了Yii 基本 url 重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 php 新手,mvc 新手,yii 新手,以及 url 重写新手.所以,很抱歉,如果我问的是一些非常基本的问题.

I am new to php, new to mvc, new to yii, and new to url-rewriting. So, I am sorry, if I am asking something very basic.

我隐藏了 index.php(来自 yii 论坛中讨论的 htaccess 方法)

I have hide the index.php (from the htaccess method discussed in yii forums)

在我的 urlmanager 中,我有这个,

In my urlmanager, I have this,

'urlFormat'=>'path',
             'rules'=>array( 
             '<controller:\w+>/<id:\d+>'=>'view',
             '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
             '<controller:\w+>/<action:\w+>'=>'<controller>/<action>' 
              ),
             'showScriptName'=>false,

我在视图/站点文件夹中有 3 个文件.

I have 3 files in view/site folder.

  1. '旅程',
  2. '邀请',
  3. 'linkedin'

现在,我的主页应该重定向到journey"操作(即应该打开site/journey.php")

Now, my home page should redirect to 'journey' action (i.e. should open the 'site/journey.php')

所以,我想,这将是

'/' => 'site/journey'

它也有效.

现在,我希望 'journey/invite' 应该调用 'invite' 操作,即应该打开 'site/invite.php'

Now, I want 'journey/invite' should invoke the 'invite' action i.e. should open 'site/invite.php'

并且,journey/linkedin"应该调用linkedin"操作,即site/linkedin.php".

And, 'journey/linkedin' should invoke the 'linkedin' action i.e. 'site/linkedin.php'.

但是,

'journey/invite' => 'site/invite',
'journey/linkedin' => 'site/linkedin'

不工作.

另外,谁能帮我理解一下,

Also, can someone help me understand this,

<controller:\w+>/<id:\d+>

即url 中的控制器是什么,w+"是什么意思?

i.e. what is controller in url and what does 'w+' mean ?

参考指南也会有所帮助.

A reference to guide will help too.

根据 bool.dev 的建议进行

更改代码,如您所说(我之前也尝试过,删除了所有默认规则).现在我的网址管理器就像,

Changed the code , as you said (I tried that earlier too, removing all default rules). Now my url manager is like,

    '/' => 'site/journey',
    'journey/invite' => 'site/invite',
    'journey/linkedin' => 'site/linkedin',
    '<controller:\w+>/<id:\d+>'=>'view',
    '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
    '<controller:\w+>/<action:\w+>'=>'<controller>/<action>', 

但是会报错

"警告:require_once():open_basedir 限制生效.文件(/var/xyz.com/../yii/framework/yii.php)不在允许范围内路径:(/usr/share/php:/usr/share/pear:/usr/share/php/libzend-framework-php:/var/*/tmp:/var/xyz.com)在/var/xyz.com/journey.php 第 12 行警告:require_once(/var/xyz.com/../yii/framework/yii.php):打开失败流:在第 12 行的/var/xyz.com/journey.php 中不允许操作致命错误:require_once():需要打开失败'/var/xyz.com/../yii/framework/yii.php'(include_path='.:/usr/share/php:/usr/share/php/libzend-framework-php')在/var/xyz.com/journey.php 第 12 行'

"Warning: require_once(): open_basedir restriction in effect. File(/var/xyz.com/../yii/framework/yii.php) is not within the allowed path(s): (/usr/share/php:/usr/share/pear:/usr/share/php/libzend-framework-php:/var/*/tmp:/var/xyz.com) in /var/xyz.com/journey.php on line 12 Warning: require_once(/var/xyz.com/../yii/framework/yii.php): failed to open stream: Operation not permitted in /var/xyz.com/journey.php on line 12 Fatal error: require_once(): Failed opening required '/var/xyz.com/../yii/framework/yii.php' (include_path='.:/usr/share/php:/usr/share/php/libzend-framework-php') in /var/xyz.com/journey.php on line 12'

当我做 xyz.com/journey/invite 甚至 xyz.com/journey

这是一个权限问题,@bool.dev 建议将特定规则放在首位 :)

It was a permission issue, @bool.dev's suggestion to put specific rules on top worked :)

推荐答案

这两个:

'journey/invite' => 'site/invite',
'journey/linkedin' => 'site/linkedin'

无法正常工作,因为该 url 与之前的规则匹配:

are not working because the url is being matched by a previous rule:

'<controller:\w+>/<action:\w+>'=>'<controller>/<action>'

为了防止这种情况发生,请确保在任何特定(即不与正则表达式匹配)重写之前最后提到规则,因此您的规则可能看起来像这样:

To prevent that from happening just make sure that rule is mentioned last, before any specific(i.e not matched by regular expressions) rewrites, so your rules can look somewhat like this :

'journey/invite' => 'site/invite',
'journey/linkedin' => 'site/linkedin',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>'

This : \w+ 表示匹配任何出现一次或多次任何单词"字符(az 0-9 _)的字符串,\d+ 表示匹配任何字符串一次或多次出现任何数字 (0-9) .查看 php 正则表达式.

This : \w+ means match any string with one or more occurrences of any "word" character (a-z 0-9 _) and \d+ means match any string with one or more occurrences of any digits (0-9) . Check out php regex.

编辑

之前没有仔细阅读您的问题,规则中的 controller 只是匹配表达式的名称,因此您可以使用 '<contr:\w+>/<act:\w+>'=>'<contr>/<act>'.

Hadn't read your question thoroughly before, the controller in the rule is simply a name for the matched expression, so you could have had '<contr:\w+>/<act:\w+>'=>'<contr>/<act>'.

Edit2

在使用规则数组阅读您编辑的问题后,afaik您可以使用 ''='=>'site/journey' 而不是 '/'=>'网站/旅程'.

After reading your edited question with the rules array, afaik you could use ''=>'site/journey' instead of '/'=>'site/journey'.

这篇关于Yii 基本 url 重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆