Laravel 过滤器通过并去请求 URL

Laravel Filter passes and going to requested URL

我正在尝试在我的应用程序中设置 laravel 过滤器。

这是我的代码,

Filter.php

Route::filter('userid', function($route,$request,$value)
{
 $id = Auth::user()->id;
 $param = $route->getParameter('id');

    $count = DB::table('timesheets')
    ->where('timesheet_id','=',$param)
    ->where('timesheet_user','=',$id)
    ->count();

   if($count == 1)
   {
    return "here going to requested url";
   }
   else
   {
    return "no permission to this page";
   }

});

我的路线,

Route::group(array('before'=>'userid:200'),function($route)
 {
    Route::get('edit-timesheet-{id}',array('as'=>'edit_timesheet_form','uses'=>'TimesheetController@editEntry'));
});

我正在获取参数并使用 filter.php 检查没问题。

但是当用户点击edit-timesheet-756时,如果过滤成功,则需要继续执行URL,如何我能解决这个问题吗

感谢和问候

在过滤函数中你应该这样写:

if($count != 1)
 {
  return Redirect::to('another_page');
 }

而不是

 if($count == 1)
 {
   return "here going to requested url";
 }
 else
 {
   return "no permission to this page";
 }

因此,如果路由可以通过过滤器,用户将被定向到您的 edit-timesheet-.. 页面,否则用户将被重定向到另一个页面,如登录页面或其他页面。

在过滤器中你只担心失败。如果检查失败,请重定向他们或给他们一个错误页面。

如果过滤器不中断请求,请求将按预期继续。

因此,您将在路线上设置过滤器。 它检查计数。如果失败,显示错误页面,否则什么都不做。

页面按预期加载。

终于找到解决办法了。

只给出真实情况下的空白

 if($count == 1)
 {
   // here left as blank
 }
 else
 {
  return "no permission to this page";
 }