如何将url的参数发送到filter.php并在那里检索?

时间:2014-10-31 22:29:05

标签: php laravel-4 laravel-routing

如何将网址的参数发送到filter.php并在那里检索?

路线

Route::get('/users/{id}/edit', 'UsersController@edit');

我想将上述网址中的{id}发送到filter.php并在那里检索它的值

类似这样的事情

Route::filter('access', function($id)
    {
        if (Auth::check())
        {
            if (Auth::user()->is_admin != 1 && Auth::user()->id = $id) {
                return View::make('users.noaccess');
            }
        }
        else
        {
            return Redirect::guest('/')->with('error', 'Please login to access this page');
        }
    });

然后使用beforeFilter将过滤器绑定到方法

$this->beforeFilter('access', array('only' => 'edit'));

1 个答案:

答案 0 :(得分:1)

过滤器闭包函数接受许多参数(http://laravel.com/docs/4.2/routing#route-filters)。您可以像这样重写您的过滤器:

Route::filter('access', function($route) {
   $id = $route->parameter('id');
   if (Auth::check()) {
        if (Auth::user()->is_admin != 1 && Auth::user()->id = $id) {
            return View::make('users.noaccess');
        }
   } else {
        return Redirect::guest('/')->with('error', 'Please login to access this page');
   }
});