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

Is it possible to access SlimApp or SlimContainer or Route in middleware? #24

Open
JanMikes opened this issue Dec 5, 2018 · 11 comments

Comments

@JanMikes
Copy link
Contributor

JanMikes commented Dec 5, 2018

Hi!

Let's have example code (from https://www.slimframework.com/docs/v3/cookbook/enable-cors.html):

$app->add(function($request, $response, $next) {
    $route = $request->getAttribute("route");

    $methods = [];

    if (!empty($route)) {
        $pattern = $route->getPattern();

        foreach ($this->router->getRoutes() as $route) {
            if ($pattern === $route->getPattern()) {
                $methods = array_merge_recursive($methods, $route->getMethods());
            }
        }
        //Methods holds all of the HTTP Verbs that a particular route handles.
    } else {
        $methods[] = $request->getMethod();
    }

    $response = $next($request, $response);


    return $response->withHeader("Access-Control-Allow-Methods", implode(",", $methods));
});

It is okay if i place this into my index.php where i have application in variable $application = $applicationFactory->create();

But i did not find if i wanted to create CORSMiddleware, register it as service and do it all via config.

The problem is $this->router - i do not have access to router in middleware service.

When debugging, i found out that Slim\Container is in the $next variable, should i use it? Any idea how to achieve it?

@ghost
Copy link

ghost commented Dec 7, 2018

Not sure what $this is referring to. In case of that example, there is no obvious context other than the whole global context of the file.

@JanMikes
Copy link
Contributor Author

It seems that it is the Slim\Container

@ghost
Copy link

ghost commented Dec 14, 2018

I see.

One nasty solution can be something like this:

    /**
     * @var SlimApp
     */
    private $slimApplication;

    public function __construct(SlimApplicationFactory $applicationFactory)
    {
        $this->slimApplication = $applicationFactory->create();
    }


    public function dispatch()
    {
        $container = $this->slimApplication->getContainer();
        $this->slimApplication->add(...);
        // ...
        $this->slimApplication->run();
    }

I am not sure if an order of the middlewares will be correct but it can be the starting point of figuring it out. But its not nice.

@ghost
Copy link

ghost commented Dec 14, 2018

Another approach can be to have SlimApp (its container respectively) somehow accessible from Nette DI and inject it into Middleware class.

@ghost
Copy link

ghost commented Dec 14, 2018

(To be honest, I do not have a solution, I am just thinking loudly basically.)

@JanMikes
Copy link
Contributor Author

JanMikes commented Jan 8, 2019

Hi, getting back to this (I feel like a chess player, just taking a long time to think about next move 😄 ).

Another approach can be to have SlimApp (its container respectively) somehow accessible from Nette DI and inject it into Middleware class.

That could work and I would consider it to be quite a clean solution.

I will check and if I am able to do it, I will send a PR.

@ghost
Copy link

ghost commented Jan 9, 2019

Quick idea: Router should be dependency of the SlimApp (created or provided into SlimAppFactory) Then router can be dependency of the cors middleware

@ghost
Copy link

ghost commented Jan 9, 2019

// This may require changes in Slim lib itself.

@ghost
Copy link

ghost commented May 6, 2019

#31

@Zemistr
Copy link

Zemistr commented May 8, 2019

@JanMikes
I moved \Slim\Router to \BrandEmbassy\Slim\RouterFactory.
Now you can inject \Slim\Interfaces\RouterInterface where you need. 😉
Please check the changes inside #31

@JanMikes
Copy link
Contributor Author

JanMikes commented May 8, 2019

Nice, thank you!

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