如何在UrlManager Yii框架中编写规则

时间:2018-02-16 19:35:49

标签: php yii url-rewriting yii2 yii-url-manager

我想从网址&#34; example.com/menu-burger-king" << /强>

以下是 actionMenu

中的代码
$url=isset($_SERVER['REQUEST_URI'])?explode("/",$_SERVER['REQUEST_URI']):false;     
    if(!is_array($url) && count($url)<=0){      
         $this->render('404-page',array(
           'header'=>true,
          'msg'=>"Sorry but we cannot find what you are looking for"
        ));         
        return ;
    }           
    $page_slug=$url[count($url)-1];
    $page_slug=str_replace('menu-','',$page_slug);
    if(isset($_GET)){               
        $c=strpos($page_slug,'?');
        if(is_numeric($c)){
            $page_slug=substr($page_slug,0,$c);
        }
    }           
    $page_slug=trim($page_slug);

我已在 config.php 文件中实施了规则,如下所示:

        'urlManager'=>array(
        'class' => 'UrlManager', 
        'urlFormat'=>'path',            
        'showScriptName'=>false,            
        'rules'=>array(     
            '' => 'store/index',
            '<action:('.$patern.')>' => 'store/<action>',
            '<controller:\w+>/<id:\d+>'=>'<controller>/view',
            '<controller:\w+>'=>'<controller>/index',   '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
            '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
        )           
    ),

这是$ pattern

$patern="cuisine|signup|signin|merchantsignup|contact|searcharea";
$patern.="|menu|checkout|paymentoption|receipt|logout|paypalinit|paypalverify";
$patern.="|OrderHistory|Profile|howItWork|forgotPassword|PageSetlanguage|stripeInit";
$patern.="|MercadoInit|RenewSuccesful|Browse|PaylineInit|Paylineverify|sisowinit";
$patern.="|PayuInit|BankDepositverify|AutoResto|AutoStreetName|AutoCategory|PayseraInit";
$patern.="|AutoFoodName|Confirmorder|Paymentbcy|Bcyinit|EpayBg|EpyInit";
$patern.="|GuestCheckout|MerchantSignupSelection|MerchantSignupinfo|CancelWithdrawal";
$patern.="|ATZinit|DepositVerify|Verification|Map|GoogleLogin|AddressBook";
$patern.="|AutoZipcode|AutoPostAddress|Item|Ty|EmailVerification|MyPoints|BtrInit|setlanguage";
$patern.="|mollieinit|mollieprocess|home|molliewebhook";
$patern.="|ip8init|ipay88verify|ipay88receiver";
$patern.="|monerisinit|confirmorder|rzrinit|rzrverify|acceptorder|declineorder|cart|restaurant";
$patern.="|voguepaynotify|voguepaysuccess|voguepayfailed|voginit|vognotify|voginit|vogsuccess";
$patern=strtolower($patern);

前端链接是:

<a href="<?php echo Yii::app()->createUrl("/menu-". trim($val['restaurant_slug']))?>">

当我尝试从控制器中删除菜单 - 并查看它时会出现致命错误。

致命错误:未捕获的异常&#39; CHttpException&#39;消息&#39;无法解析请求

1 个答案:

答案 0 :(得分:0)

这样的事情:

'menu-<action:[a-zA-Z0-9\-]+>' => 'store/<action>',

假设“汉堡王”是StoreController中的动作 - 如果这不是您的意图,请添加特定要求。 (根据需要调整正则表达式。)