如何隐藏控制器名称和yii2中的动作名称

时间:2015-08-05 05:12:17

标签: url yii2

任何人都可以建议我如何隐藏控制器和放大器来自yii2网址的动作名称? 我尝试写规则但没有奏效。 这是我的锚标记:

 NSString* stringToPaste = [NSString stringWithUTF8String:[YOUR_NSData bytes]];

我目前的网址是这样的:

 <?php echo Html::a($model->title, ['category/view/', 'type' => $model->category->urlValue,'parameter' => $model->urlValue]); ?>

但我想这样:

http://localhost/project/category/view/news-and-events/dosarrest-strong-performer-in-2015-forrester-wave-for-ddos-service-providers-1

2 个答案:

答案 0 :(得分:3)

它最终通过在main.php文件中编写规则来实现,如下所示:

'<type:[A-Za-z0-9-]+>/<param:[A-Za-z0-9 -_.]+>' => 'category/view',

答案 1 :(得分:1)

你可以创建自己的UrlRule。类似的东西:

class CustomUrlRule extends Object implements UrlRuleInterface {
    public function createUrl($manager, $route, $params)
    {
        $parts = explode('/', $r);
        if ($route === 'category/view' 
            && isset($params['type'])
            && isset($params['parameter'])
        ) {
            $url = generate some url;
            unset($params['view'], $params['parameter']);
            if (count($params)) {
                $url .= '?' . http_build_query($params);
            }

            return $url;
        }

        return false; 
    }
    public function parseRequest($manager, $request)
    {
        //parse request url and return true if it's url for category/view 
    }
}

并且不要忘记添加到配置

config/web.php:
...
'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        [
        'class' => 'app\components\CustomUrlRule',
        ],
    ...
    ],
...