我有这个博客资源,它具有常用的 CRUD 方法。(索引、创建、存储、显示、编辑、更新、销毁)。
我的 routes.php 中有以下路线:
Route::resource('blog', 'PostsController');
但我想限制除索引和显示之外的所有内容。 所以我有
Route::get('blog', 'PostsController@index');
Route::group(array('before' => 'auth'), function()
{
Route::resource('blog', 'PostsController');
});
哪个适合索引,但我不知道如何路由 show 方法?或者还有其他方法吗?我应该单独路由每个 URI 并将我想要限制的那些放在我的受限访问路由中,而不是路由资源吗?
干杯
最佳答案
Laravel 有一项功能,允许您使用 $this->beforeFilter
在 Controller 的 __construct
方法中指定过滤器。此函数采用第二个参数,让您提供异常(或仅为某些方法启用过滤器)。尝试使用您的原始路由文件并像这样设置您的 Controller :
class PostsController extends BaseController {
function __construct() {
// ...
$this->beforeFilter('auth', array('except' => array('index', 'show')));
// ...
}
// ...
请参阅 Laravel documentation 中的 Controller 过滤器.它的文档并不完整,但您也可以从 here 开始更深入地了解 Laravel 的内部结构。 .
关于php - Laravel 4 身份验证。限制对资源的某些功能的访问,但不是全部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18646482/