dtt5830659 2015-01-28 03:18
浏览 60
已采纳

根据参数值Yii重写路径

I have several rules in Yii that allows me to rewrite some routes, where every will be pass to the action as a get parameter.

'<department>' => 'products/index',
'<department>/<category>' => 'products/index',

I want to explicitly write a rule that depending of the parameter value will change the url to whatever I want

example, right now I have an URL like this www.mysite.com/Books+%26+Pencils which was rewritten because of this rule '<department>' => 'products/index', which is ok

I want to change that URL to www.mysite.com/books-pencils , if anyone know how to write a rule that compares the value of the deparment attribute and then rewrites it to whatever I want.

THanks

  • 写回答

2条回答 默认 最新

  • douzou0073 2015-01-31 15:36
    关注

    You can use a custom class to handle you special requests. I have used sth like this, to get my custom URLs out of a database:

     'urlManager'=>array(
                'rules'=>array(
                    array(
                        'class' => 'application.components.UrlRule',
                    ),
                ),
            ),
    

    Then you create your custo class similar to this:

    <?php
    
    Yii::import("CBaseRule");
    
    class UrlRule extends CBaseUrlRule
    {
        public function createUrl($manager,$route,$params,$ampersand)
        {
            // check for my special case of URL route, if not found, then return the unchaged route
            preg_match("/^(.+)\/(.+)$/", $route, $r);
            if(!is_array($r) or !isset($r[1]) or !isset($r[2])) {
                return $route;
            }
    
            // handle your own route request, and create your url
            $url = 'my-own-url/some-thing';
    
            // check for any params, which i also want to add
            $urlParams = $manager->createPathInfo($params,"=","&");
    
            $return = trim($url,'/');
            $return.= $urlParams ? "?" . $urlParams : "";
    
            return $return;
        }
    
        public function parseUrl($manager,$request,$pathInfo,$rawPathInfo)
        {
    
            // handle my special url request
            $controller = '....';
            $action = '.....';
    
            // return the controller/action that should be used     
            return lcfirst($controller)."/".$action;
        }
    }
    

    I do not know if this was what you wanted, but at least in this class you can do everything you need with the URL requested. If you would e.g. like to redirect a lot of similar URLs with a 301 Redirect to 1 URL, you could think of sth like this in the parseUrl function

    // check my route and params, and if I need to redirect
    $request->redirect('/your/new/url/?params=bla',true,'301');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line