Get parameter passed in URL in Laravel view as variable directly? Get parameter passed in URL in Laravel view as variable directly? laravel laravel

Get parameter passed in URL in Laravel view as variable directly?


Use this code :

{{ Route::current()->getParameter('theParameterName') }}

EDIT: Above doesn't seem to be supported anymore in recent Laravel versions. You should use @lukasgeiter answer instead:

Route::input('name');


There is a nice shortcut for Route::current()->getParameter():

Route::input('name');


For small projects or simple examples, it may seem like this is a "roundabout" way, however this is the way it should be. In order to create more reusable, quality code, you need to have this separation of concerns. An over-simplified idea follows.

It is your route's job to figure out which controller needs to be called, and to make sure it is called with the correct parameters.

It is your controller's job to read the state of the application (input), communicate with the model (if needed), send the data into the view, and return the response. There's plenty opinion on whether or not this violates the Single Responsibility Principle, but no need to go into that here.

It is the view's job to use the data passed to it to build the response. The view shouldn't care where the data came from or how it was gotten, only that it now has it and can do what it needs. Your $missionName should be able to come from a URL segment, a URL request variable, a field on a model, or any other place you can think of, but the view shouldn't know any of that.