douzhi6365 2014-01-31 18:25
浏览 48
已采纳

Laravel 4将自定义过滤器应用于路径 - 错误未定义的偏移量

Trying to prevent non-admin users from accessing certain pages. I must not be doing it correctly though because I'm getting back an error of undefined offset: 1 while trying to access an admin only page while logged in as an admin.

Filter

Route::filter('admin', function()
{
if (!Auth::user() || Auth::user()->permissions != 1) return Redirect::to('/');
});

Routes

Route::resource('deals', 'DealsController');

Route::resource('blog', 'PostsController');

Route::group(array('before' => 'admin'), function()
{
    Route::get('deals/create', 'DealsController');
    Route::get('blog/create', 'PostsController');
});

I can't put the filter on a constructor of my deals or blog controller because the index page for each of those routes needs to be accessible to all users. When I'm not logged is as an admin, the routes function correctly and redirect back to the home page when trying to visit a page that is admin-only. Thanks for any insights.

  • 写回答

1条回答 默认 最新

  • duanqu9279 2014-01-31 21:20
    关注

    Just solved the problem, it was occurring because of the routing. Trying to apply a route group with a filter to routes that had already been defined as a resource. When I removed the group routes and did some research on using the beforeFilters in a constructor, I found that I can exclude a function, thus solving my problem.

    This line of code was the solution

    $this->beforeFilter('admin', array('except' => 'index'));
    

    I hadn't know that I could use the except property, but now it all works well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入