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 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助