使用 Yii 框架重写 URL [英] URL rewriting with Yii framework

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

问题描述

我有一个类似的网址

www.example.com/food/xyz

为此我在 main.php(config 文件夹)中编写了这样的规则

for which I have written a rule like this in the main.php (config folder)

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

这意味着内部xyz"将根据规则作为参数传递给我的 FoodController 的 actionIndex

This means that internally "xyz" will be passed as a parameter to the actionIndex of my FoodController according to the rule

class FoodController extends Controller
{
    public function actionIndex($name){
        //some code here
    }

    public function actionItems(){
        //some code here
    }
}

我面临的问题是,当我使用 URL 时,我在同一个类中有另一个方法,称为 actionItems

The problem i'm facing is that I have another method in the same class called actionItems and when I use the URL

www.example.com/food/items

它调用 actionIndex 并将items"作为参数传递

it calls the actionIndex and passes "items" as a parameter

知道如何解决这个问题吗?

Any idea on how to solve this?

提前致谢.

推荐答案

你的第一条规则应该是项目

Your first rule should be for the items

'/food/items'=>'food/items',
'/food/<name:\w+>/' => 'food/index/',

或者,我宁愿使用别名来拆分控制器的使用,以便将所有 'food'/<:name>' url 与 '/food/action' url 区分开来,例如:

Alternatively, I would rather use an alias to split the use of the controller such that all 'food'/<:name>' urls are differentiated from '/food/action' urls for example:

'/food/<name:\w+>/' => 'food/index/',
'/foods/<action:\w+>' => 'food/<action>'

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

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