我怎么能路由到app / webroot文件夹? (CakePHP的)

时间:2013-04-27 17:20:23

标签: php cakephp cakephp-2.3

如果我输入:“http://examplepage.com/gallery/examplecagegory/1-test-picture.jpg”到浏览器。

转到webroot: “应用程序/根目录/画廊/图片/ 1.JPG”

我试过了:

Router::connect('/gallery/:slug_category/:id-:slug.:extension',
                array('webroot/gallery/pictures'),
                array(
                    'pass' => array('id', 'slug'),
                    'id' => '[0-9]+'
                    )
                );

但我坚持第二排......: - /

1 个答案:

答案 0 :(得分:5)

这不是您可以使用路由做的事情,因为the book声明:

  

路由是一种将网址映射到控制器操作的功能。

图像资源不是控制器操作。您应该在app/webroot的.htaccess文件中使用普通的RewriteRule来重写所有调用。这样的事情可以解决问题:

RewriteRule ^gallery/[a-z]+/([0-9]+)-[a-z-]+\.([a-z]{3})$ /gallery/pictures/$1.$2

请注意,默认情况下HtmlHelper会在app/webroot/images文件夹中搜索图像,因此您需要使用绝对URL(使用前导斜杠为所有图像调用添加前缀)来使用您的重写路径,例如这不起作用

$this->Html->image('gallery/examplecategory/1-test-picture.jpg');

你应该改用:

$this->Html->image('/gallery/examplecategory/1-test-picture.jpg');