【问题标题】:Url mapping problems using Yii使用 Yii 的 URL 映射问题
【发布时间】:2013-04-24 08:57:52
【问题描述】:

我有静态资源的控制器,例如图像

class ResourceController extends Controller {

public function actionImage($fname) {
    $file = Yii::app() -> params['baseFilesPath'] . $fname;
    if (file_exists($file)) {
        header("Content-Type: image/jpeg");
        header("Accept-Ranges: bytes");
        header("Content-Length: " . filesize($file));
        // header("Content-Disposition: attachment; filename=" . $fname);
        readfile($file);
    }
}

我有这个控制器的 url 映射:

urlManager'=>array(
        'urlFormat'=>'path',
        'rules'=>array(
            'resources/<fname:\w+>' => 'resource/image',
            'product/<name:\w+>/<id:\d+>' => 'catalog/product', 
            '<controller:\w+>/<id:\d+>'=>'<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
            '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
        ),
    ),

Yii 为这个映射生成正确的 url:

/index.php/resources/bf81346e36131b5740bf96dc19b70ac0.jpeg

但是控制器没有被调用。

你能帮帮我吗?我认为我的正则表达式是错误的。

【问题讨论】:

    标签: url yii mapping


    【解决方案1】:

    由于您的fname 参数中有一个dot,并且dot 不是'word' character (\w),您应该简单地尝试以下规则:

    'resources/<fname>' => 'resource/image',
    

    http://www.yiiframework.com/doc/guide/1.1/fr/topics.url#using-named-parameters

    如果省略ParamPattern,则表示参数应该 匹配除斜线 / 以外的任何字符 /

    【讨论】:

      猜你喜欢
      • 2011-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-01
      • 2017-10-02
      • 1970-01-01
      • 2012-09-14
      相关资源
      最近更新 更多