doushan6161 2014-10-20 13:54
浏览 26
已采纳

Laravel定制过滤器

I need to be able to access a number of routes only if a condition is met. Else I should not have access to those routes.

I thought this should be done with a filter but I think I'm missing something about how they work.

So this is my filter:

Route::filter('my.filter', function()
{
    //some code regarding said condition
    if($mycondition==true){
        //WHAT TO PUT HERE?
    }else{
        //Error message
    }
}

And in my routes I will have:

Route::group(array('before' => 'my.filter'), function()
{
    Route::resource('cities', 'CitiesController');
    //... many more controllers here
});

But all the examples I have seen have a redirect inside the filter in the if part. I don't want that, I only want, if the condition in the filter is true, you get to see that url.

  • 写回答

2条回答 默认 最新

  • duanbushi1867 2014-10-20 14:06
    关注

    If you wanna have your filter stop further Execution of your routes (and controllers and so on) you have to:

    • Return something (e.g. a Redirect)
    • or throw an Exception

    In Laravel you can do this pretty easily:

    Route::filter('my.filter', function()
    {
        //some code regarding said condition
        if($mycondition==true){
            // here you have to do nothing so you could also flip the if...
        }else{
            App::abort(403);
        }
    }
    

    Official Docs

    Little sidenote: As mentioned above in the comment i would flip the if. Like that:

    Route::filter('my.filter', function()
    {
        //some code regarding said condition
        if($mycondition==false){
            App::abort(403);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?