我使用的是启用了ORM/Auth模块的Kohana 3.1。我想提供静态文件(文档、pdf等)仅限于A_only目录中角色为A的用户。
由于.htaccess文件只提供它直接找到的URL,而不是传递给index.php,所以我可以通过.htaccess拒绝A_only中的所有访问,但是我如何在控制器函数中提供静态文件呢?
我还可以在A_only目录中有一个需要身份验证的.htaccess。但是,这将要求他们重新登录,即使我将其设置为在数据库中查找用户/密码。
发布于 2011-06-08 17:58:11
你需要告诉你的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))剩下的都是样板文件。
https://stackoverflow.com/questions/6273587
复制相似问题