How to build a Laravel route that requires a specific URL query parameter?

允我心安 提交于 2019-12-05 09:27:46
The Alpha

Laravel doesn't use the query part of a uri for routing, for localhost/admin/users?data=refresh you may use something like this:

Route::get('admin/users', function(){
    $data = Input::get('data');
});

You can make a request to the route using localhost/admin/users?data=refresh. You can declare your route like this:

Route::get('admin/users' , array('before' => 'ajax:data', 'as' => 'admin.users', 'uses' => 'Admin\Users\UsersController@dataRefresh'));

Here, refresh is passed to route filter and is available in third argument ($param) so you can retrieve refresh in $param. Create the filter as given below:

Route::filter('ajax', function($route, $request, $param){

    // This will give query string 'refresh'
    // if you passed it as http://domain.com?data=refresh
    $data = $request->get($param);

    // You can retrieve the $param, third argument
    // if you pass a parameter, i.e. 'refresh'
    // param will contain 'refresh'
});

I think the closest you will get to what you want is Route::input.

http://laravel.com/docs/routing#route-parameters

Accessing A Route Parameter Value

If you need to access a route parameter value outside of a route, you may use the Route::input method:

Route::filter('foo', function()
{
    if (Route::input('id') == 1)
    {
        //
    }
});

I would not personally do it this way myself, I would just check for the parameter within the controller and if it matches then perform the refresh or use a admin/users/refresh route instead.

 Route::get('admin/users/{data?}' , array('before' => 'ajax', 'as' => 'admin.users', 'uses' => 'Admin\Users\UsersController@dataRefresh'));

And you can

/admin/users/refresh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!