5

Let's say I have URLs like this:

  1. localhost/admin/users/ <--- Main Admin Users page
  2. localhost/admin/users/?data=refresh <---- A typical ajax request made from that page

And a simple controller like this:

class UsersController extends Controller {

     public function index()

         // call some services
         // return a view
     }

     public function dataRefresh {

         // call some services
         // return some JSON
     }
}

And here's my routes.php I'm working on:

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

What can I do in my second route to require a URL query parameter ?data and furthermore require it is set to data=refresh? And how do I ensure it doesn't conflict with the other route?

Note: I'm aware this may not be considered "pretty URL" by some. I do implement pretty URLs / slugs when appropriate, however I also think there are many cases where the query parameters are more clearer and cleaner (ie. give a user a clear understanding of what part of the page's URL is for filtering the data in a datagrid...and assures a user the parameters can be removed without causing the page to break or go missing). Google does this themselves, as well as many other reputable sites.

Note: I have applied an ajax route filter to the second route. I've also set the route to point towards the dataRefresh method in my controller.

This is as far as I've got. Any ideas?

tereško
  • 58,060
  • 25
  • 98
  • 150
prograhammer
  • 20,132
  • 13
  • 91
  • 118

3 Answers3

3

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'
});
prograhammer
  • 20,132
  • 13
  • 91
  • 118
The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • Do I perform a check inside the route and call the appropriate controller@method? If the input exists and it's an ajax request then call UsersController@dataRefresh ??? I would need to put all my ajax request/parameter checks in here for the admin/users page, right? – prograhammer Mar 13 '14 at 21:32
  • You can check it inside filter. – The Alpha Mar 13 '14 at 21:37
  • Yeah, ok yeah...check the parameter in the filter. One last detail...what if I want to pass the value as well? Like this `'before' => 'ajax:data:refresh'`, then are these passed into the filter function as 3rd and 4th arguments? Almost there!! – prograhammer Mar 13 '14 at 21:43
  • The word `data` or `refresh` should not appear in the filter at all, this filter should just be taking first and second argument and then performing the checks. The params `data` and `refresh` should be entered into the route line of code...please clarify in your answer for me, and I can accept it. Thanks so much for the help! – prograhammer Mar 13 '14 at 21:46
  • You may pass it using `ajax:data=value` then explode it using `explode('=', $param)` and you'll get an array, `0` is `data` and `1` is `value`. – The Alpha Mar 13 '14 at 21:46
  • Could you replace your whole answer, and just put an example route, and an example filter....maybe add a second route line for the non-ajax regular page route (just to show they can co-exist). I think this answer will get a lot of upvotes! I'll accept it regardless, just thought that would help a lot of folks. – prograhammer Mar 13 '14 at 21:49
  • Can you reword your question on comment, make me clear. – The Alpha Mar 13 '14 at 21:49
  • Give a full example. Your answer right now is very cluttered. You just need to rewrite the whole answer. Simply show a route and a route filter. Just finish the code. Including the part about `ajax:data=refresh` and the explode. I think you've got a great solution, but other's may not easily see this without redoing your answer. Just my suggestion, that's all. – prograhammer Mar 13 '14 at 21:51
  • I edited your answer...does it make sense what I was expressing now? – prograhammer Mar 13 '14 at 22:39
  • Thanks but I didn't see any changes. – The Alpha Mar 13 '14 at 22:45
0

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.

SamV
  • 7,548
  • 4
  • 39
  • 50
  • Can I make a filter that works for any parameter? In other words, can I pass a parameter name into the filter and the filter takes that name and does a check to see that it's in the Route::input? ie. `'before' => 'myfilter:data'`??? And how to pass a second argument for the value? – prograhammer Mar 13 '14 at 21:28
0
 Route::get('admin/users/{data?}' , array('before' => 'ajax', 'as' => 'admin.users', 'uses' => 'Admin\Users\UsersController@dataRefresh'));

And you can

/admin/users/refresh

brunocascio
  • 1,867
  • 3
  • 14
  • 21