您将需要告诉您的 Web 服务器停止处理静态文件。最简单的解决方案是将静态文件移到 web 目录之外,这样 Apache 就找不到它们了;这将迫使该请求通过 Kohana。
第二部分是创建一个控制器,它为您处理权限和文件发送。Kohana 用户指南有一个相当不错的示例,供您使用:
Controller_Userguide 第 247 行
// 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))
其余的是样板。