Hi,
All cases i find about middleware explain it through users, user roles, permissions etc. Does anyone knows an example on how to use middleware only to break down an url into variabls and pass that through to the function called?
For example, if you have:
Route::get('foo/{param1}/{param2}/thing-{thing1}/thing-{thing2}', ['uses'=>'FooController@fooByParamAndThings']);
Route::get('bar/{param1}/{param2}/thing-{thing1}/thing-{thing2}', ['uses'=>'BarController@barByParamAndThings']);
You can make the functions in the two controllers:
FooController:
public function fooByParamAndThings($param1,$param2,$thing1,$thing2){
if($param1 = something){
$params = $param1.$param2;
}
if($thing1= somethingelse){
$things = $thing1.$thing2;
}
$foo= \App\Foo::where('param', $param)->where('thing', $thing)->firstOrFail();
return view('foo', ['foo'=>$foo]);
}
BarController:
public function barByParamAndThings($param1,$param2,$thing1,$thing2){
if($param1 = something){
$params = $param1.$param2;
}
if($thing1= somethingelse){
$things = $thing1.$thing2;
}
$bar= \App\Bar::where('param', $param)->where('thing', $thing)->firstOrFail();
return view('bar', ['bar'=>$bar]);
}
This should work.
But:
Wouldn't it be much better to use a ParamThings middleware that performs all the actions on the params and things, and only passes the results to the functions? now the actions on that params and things are duplicate in the code.
But how to get the url variables to the middleware, and how to pass the $params and $things to the function?
Bart
calling Route::getCurrentRoute()->parameters()
should return a key value array of url parameters for the current route
and how do you pass the result of your middleware to the function? because what i see in the user and access examples are only stopping the route or going to next if everything is fine, but how to pass new made data from the middleware to the next/controller/function?
i found this one, think this would work for us: http://stackoverflow.com/a/31454023
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community