Support the ongoing development of Laravel.io →
Configuration Architecture

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

Last updated 3 years ago.
0

calling Route::getCurrentRoute()->parameters() should return a key value array of url parameters for the current route

Last updated 8 years ago.
0

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?

0

i found this one, think this would work for us: http://stackoverflow.com/a/31454023

Last updated 8 years ago.
0

yes, it works. for who ends up in this tread by googling this ;)

0

Sign in to participate in this thread!

Eventy

Your banner here too?

BartHuis barthuis Joined 19 May 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.