过滤器失败前未过滤过滤器后的路径

时间:2015-01-06 19:31:16

标签: laravel-4 laravel-routing

根据docs,当路由的前置过滤器失败时,后过滤器被取消,但似乎不是这种情况。我从数据库中取出导航。我在过滤之前的路由是在Route :: group还是普通路由上检查一个人是否访问了某个页面。如果没有,它将返回Redirect :: route('new route')。

过滤后的路线会向visit_pages表添加一行。

当我点击未访问其先决条件页面的链接时会发生什么,是否会重定向。但是 - 它仍然会向数据库添加一行。所以后来没有被取消。它仍然在发射。

我测试的方式是我登录并在页面上。清除了数据库中的页面访问。然后我点击了我的“教室”导航项目。这需要“定位”

输入数据库的内容是按以下顺序进行页面访问:

  1. 教室
  2. 课堂教学指导
  3. 取向
  4. 我期待看到的是:

    1. 取向
    2. 路线

      Route::group(array("prefix"=>"classroom","before"=>"checkPrerequisite"),function()
      {
          Route::get('/',array(
              'as'=>'classroom',
              'uses'=>'ClassroomController@index',
              'after'=>'addvisit',
              ));
          //there are more routes here, but they don't need after filters.
          Route::get('/instructions',array(
              'as'=>'classroom.instructions',
              'after'=>'addvisit',
              function()
              {
                  return View::make('classroom.instructions');
              }
          ));
      });
      

      过滤前

          Route::filter('checkPrerequisite', function($route, $request)
      {
          $sPrerequisite = Navigation::where('url','=',Navigation::where('url','=',Route::currentRouteName())->first()->prerequisite)->first();
          // get the module id from session
          $mod_id = Session::get('current_module');
          // get the page from the user_page_visits
          $page = Auth::user()
                          ->pages()
                          ->where('module_id','=',$mod_id)
                          ->where('nav_id','=',$sPrerequisite->id)
                          ->first();
      
          if(!$page) return Redirect::route($sPrerequisite->url);
      });
      

      过滤后

      Route::filter('addvisit', function($route, $request, $response)
      {
          // get the current route
          $current = Route::currentRouteName();
          // get the id of the navigation item with this route
          $nav_id = Navigation::where('url','=',$current)->first()->id;
          // get the module id from cache
          $mod_id = Session::get('current_module');
          // see if the page has been visited
          $page = Auth::user()
                          ->pages()
                          ->where('module_id','=',$mod_id)
                          ->where('nav_id','=',$nav_id)
                          ->first();
      
          if($page)
          {
              // if it has been visited, increment the visits column by 1
              $page->increment('visits');
          }
          else
          {
              // otherwise, create a new page visit
              $visit = new UserPageVisits;
              $visit->user_id = Auth::user()->id;
              $visit->module_id = $mod_id;
              $visit->nav_id = $nav_id;
              $visit->save();
          }
      });
      

0 个答案:

没有答案