Back

[L5.4] Middleware in controller construct not working


gzai posted 5 days ago

Hello,

I have code like this

class AController extends Controller
{

    public function __construct()
    {

        echo 'outside - AController';
        echo '<br/>';
        
    	$this->middleware(function ($request, $next) {

                echo 'inside - AController';
                echo '<br/>';

		return $next($request);
	});
        
    }

    public function handle()
    {

        $B = new BController;
        if ( $B->check() ) {
                $controller = BController::class;
                $action = 'index';
        } else {
                $controller = BController::class;
                $action = 'nothing';
        }

        $container = app();
        $route = $container->make(Route::class);
        $controllerInstance = $container->make($controller);

        return (new ControllerDispatcher($container))->dispatch($route, $controllerInstance, $action);

    }

}


class BController extends Controller
{

    public function __construct()
    {

        echo 'outside - BController';
        echo '<br/>';
        
    	$this->middleware(function ($request, $next) {

                echo 'inside - BController';
                echo '<br/>';

		return $next($request);
	});
        
    }

}

When BController call from AController, in the browser :

outside - AController

inside - AController

outside - BController

why, middleware in BController not showing like this?

outside - AController

inside - AController

outside - BController

inside - BController


Sign in to participate in this thread!



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