Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route specific middleware #10

Open
mtvbrianking opened this issue Mar 9, 2019 · 1 comment
Open

Route specific middleware #10

mtvbrianking opened this issue Mar 9, 2019 · 1 comment

Comments

@mtvbrianking
Copy link

I've managed to use the request-handler combined with aura-router with a single router handler.

Am trying to implement route specific middleware, as opposed to the 'global' application middleware.

$routerContainer = new RouterContainer();
$map = $routerContainer->getMap();

// Works fine...
$map->get('index', '/', 'App\Http\Controllers\HomeController::index');

// Error: Invalid request handler: array
$map->get('index', '/', [
    new SampleRouteMiddleware(),
    'App\Http\Controllers\HomeController::index'
]);

$request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);

$requestContainer = new RequestHandlerContainer();

$dispatcher = new Dispatcher([
    new SampleAppMiddleware(),
    new AuraRouter($routerContainer),
    new RequestHandler($requestContainer),
]);

$response = $dispatcher->dispatch($request);
@oscarotero
Copy link
Member

The array you're providing is not callable. A way to use an array of routes as a single callable is as the following:

$map->get('index', '/', new Dispatcher([
    new SampleRouteMiddleware(),
    'App\Http\Controllers\HomeController::index'
]));

As you can see, Dispatcher implements also RequestHandlerInterface so you can use it to group a list of callables in a single request handler instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants