Skip to content

Instantly share code, notes, and snippets.

@sdwarwick
Last active November 29, 2015 18:22
Show Gist options
  • Save sdwarwick/c508dee85d837288d149 to your computer and use it in GitHub Desktop.
Save sdwarwick/c508dee85d837288d149 to your computer and use it in GitHub Desktop.
route parameters and url query parameters
// Suppose you have a minimum of two routes, where a redirection to a route wants to include the original URL query string.
//
// The only way to add a URI query string to a route in Laravel is when you use the
// route( "named route" , "optional route parameters" ) construction.
//
// These "optional route parameters" are in the form of an array, potentially with associated keys.
//
// If the named route is a simple route, the associative array "optional route parameters" is used to build
// a URI query string.
//
// for example if you have
//
// Route::get('intendedRoute', ['as' => 'intended' , ...]
//
// and somewhere you do redirect()->route('intended',[ 'a'=>10,'b' => 20])
//
// you will get a redirect to: uri/intended?a=10&b=20
// which is what you want.
//
//
// BUT,
//
// if the named route has sub-routes, the subroute will be filled first. for example,
//
// Route::get('intendedRoute/{option}', ['as' => 'intended' , ...]
//
// the redirect()->route('intended',[ 'a'=>10,'b' => 20])
//
// will give you uri/intended/10/?b=20
//
// and you lose the first parameter of the query string.
//
// there is only one way to prevent this - it is to add a "dummy" element" to the front of the optional array.
//
// This is very fragile behaviour - it means that you have to be very careful if you add sub-routes that you are not losing the URI query parameter strings.
//
// It would seem much better for the concept of URI query strings and patn parameters to be far more separate, either through an addition to the formate of the "optional query" array, or through the addition of some other way to add the URI query parameters to routes across all of Laravel.
//
// this is the simplest code I could find that shows the behaviour that mixes route parameters with uri-query parameters.
// where "uri-query parameters" means stuff like ?this=that&etc=somethingElse and route parameters means url/this/that
//
// To see the issue, assume you have links your site that produce url query parameters, and want the default behavior of the redirect
// ( no route parameters ) to be used.
// The incoming address is: http:somesite.zzz?ad_info1=1&ad_info2=2
// in this case, the resulting redirect will be:
// http:somesite.zzz/quote/1?ad_info2=2
// what you expect is:
// http:somesite.zzz/quote?ad_info1=1&ad_info2=2
// the variable parameter routes are needed for other applications / views, and could be expanded to another layer of variabiltiy
//
// You can create a hack by pulling in the input parameters and pre-pending a null value, but this is then dependant on the route parameter
// options.
// it would be really nice to get the concepts of route parameters and url query parameters compltely separated. it would be ideal to
// be able to specify a redirect route and add url queary parameters across all different redirect constructs without requiring the
// a named route per-se
// further, the "url()" helper function would be well served by having an abiltiy to construct a url with query parameters as well.
//
// one approach would be adding a "withQuery" function to the redirect and url helper functions
// another is having an associative array with keys "routeParameters" and "queryParameters" as part of the redirect and url construct.
// there are probably other places, I am simply not that familiar with other implications.
Route::get('quote/{optional?}',
[ 'as' => 'testRoute',
function($routeOption = 'no_route_option')
{ $queryFromRequest = json_encode(Request::query());
return "route option: $routeOption queryInput: $queryFromRequest";}] // just to see the output
);
Route::get('/', function () {
$inQuery= Request::query();
//$currentQuery = array_merge( [''], $inquery );
return redirect()->route('testRoute',$inQuery); // routes must be named to get this functionality.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment