lundi 26 septembre 2016

Laravel shows parameter name instead of value (when using optional parameter in route)

I've got a form where a user can enter a start location, when submitting he will go to a map centered on this location. He can also just skip this step and go to the map directly. (this is the simplified version to skip the whole background story)

This is the first piece of code, when the user has used the form it will take the start location from the form and redirect to the map: (never mind the planning variable, it will always be 'own' for the purpose of this case)

Route::post('InitiateUserTrip', ['as' => 'InitiateUserTrip', function(WelcomeFormRequest $request) 
{
    if (Input::get('planning') == 'own') {
        return Redirect::route('Map',array('startLocation', Input::get('startLocation')));
    } else {
      return view('welcome');
    }
} ]);

This is the map route, the startLocation is optional as explained above. (API key would be my personal API KEY.) What I see happening here is that the debugbar is showing 'startLocation' instead of the value of the parameter. (name instead of value)

Route::get('Map/{startLocation?}', ['as' => 'Map', function($startLocation = null)
{
    $map = new Map('API KEY',$startLocation,'GMainMap');    
    $googleMap = $map->getMap();
    $node = new Node();
    $node->loadBySelection('ALL');
    $nodes = $node->getNodes();
    \Debugbar::info("Map: " . $startLocation);
    return View::make('map')->with('map', $googleMap)
                            ->with('nodes',$nodes)
                            ->with('startLocation',$startLocation);
}]);

I started playing with the URL to see what was going on. Assuming the user entered 'Miami' as startLocation this would result in following URL:

http://ift.tt/2d3emOm => Debugbar would show 'startLocation'

When I modify the URL myself to http://ift.tt/2dtoKwW => Debugbar would show 'Miami'

It's not just Debugbar that would show wrong contents. I try to geocode based on this variable and it fails because it sees the contents as 'startLocation' as well.

I may be able to solve this problem by creating two routes, one with and one without parameter but I suppose I'm just missing something obvious.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire