我正在使用启用了 ORM/Auth 模块的 Kohana 3.1。我想仅在目录 A_only 中向角色 A 的人提供静态文件(文档、pdf 等)。
由于 .htaccess 文件只提供它直接找到的 URL 而不会将其交给 index.php,因此我可以通过 .htaccess 拒绝 A_only 中的所有访问,但是我将如何在控制器函数中提供静态文件?
我还可以在需要身份验证的 A_only 目录中有一个 .htaccess。但是,即使我将其设置为在数据库中查找用户/密码,这也需要他们再次登录。
我正在使用启用了 ORM/Auth 模块的 Kohana 3.1。我想仅在目录 A_only 中向角色 A 的人提供静态文件(文档、pdf 等)。
由于 .htaccess 文件只提供它直接找到的 URL 而不会将其交给 index.php,因此我可以通过 .htaccess 拒绝 A_only 中的所有访问,但是我将如何在控制器函数中提供静态文件?
我还可以在需要身份验证的 A_only 目录中有一个 .htaccess。但是,即使我将其设置为在数据库中查找用户/密码,这也需要他们再次登录。
您将需要告诉您的 Web 服务器停止处理静态文件。最简单的解决方案是将静态文件移到 web 目录之外,这样 Apache 就找不到它们了;这将迫使该请求通过 Kohana。
第二部分是创建一个控制器,它为您处理权限和文件发送。Kohana 用户指南有一个相当不错的示例,供您使用:
// 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))
其余的是样板。