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 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验