Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

alekslyse's avatar

Skip route group from middleware

Is it any way to skip a route group in middleware. I want to pass a middleware to a route group, and if its false I want laravel to just skip the whole group and move on.

0 likes
9 replies
SaeedPrez's avatar

You could create a middleware and apply it to the specific route group, if the conditions are not met, then you could either redirect to another page or abort(404) to give the impression those routes do not exist.

alekslyse's avatar

I dont think that will work. I want to system to load contextual so I cant redirect or hard abort. In other words I want to have say 5 groups with the same entry points (but loading different controllers), but only one of them loaded based on a session setting.

I just wish it would be a simple function that say if this condition is true, load the group, if not ignore the group.

ctomasz's avatar

You do not need 5 different controllers but 5 different implementations of behavior for those entry points in one controller ( strategy pattern)

alekslyse's avatar

Yeah normally that would work, but not in this case as I just want to use laravel core as a first layer router, routing into second independent layers. Gathering in one controller would be a total mess.

ctomasz's avatar

Of course, you will do what you want :-)

But to clarify - by implement strategy pattern at "the one controller" I think about something like this:


// your one special controller
class SpecialController extends BaseController
{
    private $implementation;

    public function __construct()
    {
        // put here your logic to choose of particular implementation, and assign to the attribute
        $this-implementation = new SpecjalLogic1;
    }

    public function someMethod()
    {
        return $this->implementation->someMethod();
    }
}



interface SpecialLogicInterface {
    public function someMethod();
}


class SpecialLogic1 implements SpecialLogicInterface
{
    public function someMethod()
    {
        // todo
    }
}

class SpecialLogic2 implements SpecialLogicInterface
{
    public function someMethod()
    {
        // todo
    }
}
dmeganoski@gmail.com's avatar

That is one method... I use that method to share controllers, though it would be better to use a trait than to have to call $this->implementation all the time.

Here is an awesome video series here at laracasts about how to clean up code. It shows suggested uses of the different components of laravel. It may give you a few ideas. https://laracasts.com/series/whip-monstrous-code-into-shape

on another note, don't forget you can always do things like this... ;)

Route::group(array('prefix' => 'entry', 'middleware' => 'entryMiddleware'), function() {

   switch(Session::get('value')) {

       case 1:
           Route::get('this', array('as' => 'entry.this', 'uses' => 'value1\Controller@this'));
           Route::get('that', array('as' => 'entry.that', 'uses' => 'value1\Controller@that'));
           break;

       case 2:
           Route::get('this', array('as' => 'entry.this', 'uses' => 'value2\Controller@this'));
           Route::get('that', array('as' => 'entry.that', 'uses' => 'value2\Controller@that'));
           break;
       default:
           Route::get('this', array('as' => 'entry.this', 'uses' => 'default\Controller@this'));
           Route::get('that', array('as' => 'entry.that', 'uses' => 'default\Controller@that'));
           break;
   }

});
ctomasz's avatar

@dmeganoski no offense :-) , I just like to discuss ... ( and I can learn from others).

though it would be better to use a trait than to have to call $this->implementation all the time.

Could you provide some small example how to use trait in this case (in your opinion) ?

on another note, don't forget you can always do things like this... ;)

Yeah, he can do that but should he ? Assume you get a new project to maintain or add some new feature. Probably you (or the others ) expect a one entry point is assign to one controller. Then you can read logic of an application in the controller file.

dmeganoski@gmail.com's avatar

Sorry, 3 weeks later and I just remembered that I had not responded.

Actually, I had been confused about the logic of this, I'm not sure a trait would be the best in that particular case.

I was thinking he had separate base controllers which both were sharing an 'implementation', not one base controller implementing several different implementations.

I have one project that has frontend and backend controllers that reference a 'shared' controller by $this->controller. In this case it would be better to put the shared controller into trait and "use" it on the different controllers. That was where I was going with that.

That may still be one way to achieve the end result.

Please or to participate in this conversation.