您将需要告诉您的Web服务器停止处理静态文件。最简单的解决方案是将静态文件移动到Web目录之外,以便Apache无法找到它们;这将强制该请求通过Kohana。
第二部分是创建一个处理权限和文件发送给你的控制器。 Kohana的userguide有东西给你工作过相当不错的例子:
Line 247 of Controller_Userguide
// Get the file path from the request
$file = $this->request->param('file');
// Find the file extension
$ext = pathinfo($file, PATHINFO_EXTENSION);
// Remove the extension from the filename
$file = substr($file, 0, -(strlen($ext) + 1));
if ($file = Kohana::find_file('media/guide', $file, $ext))
{
// Check if the browser sent an "if-none-match: <etag>" header, and tell if the file hasn't changed
$this->response->check_cache(sha1($this->request->uri()).filemtime($file), $this->request);
// Send the file content as the response
$this->response->body(file_get_contents($file));
// Set the proper headers to allow caching
$this->response->headers('content-type', File::mime_by_ext($ext));
$this->response->headers('last-modified', date('r', filemtime($file)));
}
else
{
// Return a 404 status
$this->response->status(404);
}
你需要担心的主要是改变其中的Kohana查找文件:
if ($file = Kohana::find_file('media/guide', $file, $ext))
其余的是样板。
这工作完美,谢谢!我只是把一个.htaccess文件拒绝所有访问,然后使用一个路径将它引导到控制器,而不是从Web服务器隐藏目录。 – dualistic 2011-06-09 01:25:16