1

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

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

但是控制器没有调用。

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

4

1 回答 1

2

由于您的参数中有一个点,并且不是“单词”字符( ),因此您应该简单地尝试以下规则:fname\w

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

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

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

于 2013-04-24T09:21:29.207 回答