Change target controller/action from middleware Change target controller/action from middleware laravel laravel

Change target controller/action from middleware


It seems like your solution might be better suited in the routes file. You are suggesting serving a different route given a certain condition.

So in your routes.php file:

Route::get('test', function(){   if($condition){       return App::make('App\Http\Controllers\TestController')->index();   } else {       return App::make('App\Http\Controllers\BlaBlaController')->index();   }});

If you still want to handle it in the middleware you should be able to do the same thing as above:

return App::make($controller)->index(); // or whatever controller method you want to call.

If you need both sets of middlewares to be called, then inside the constructor (after calling your middlewares) check your condition and call the other controller like above.


If you want to change the users url, I don't think there's any way other than returning a redirect. Most users don't notice the redirect so it will probably seem "transparent."

In that case, your middleware function looks like:

public function handle($request, Closure $next){    if (something == thingy) {        return redirect('/blabla');    }    return $next($request);}