The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

How do I access slim container within Middleware class?

jameslovescode's Avatar

jameslovescode

24 Nov, 2015 10:42 AM

I have the following Middleware class:

<?php 
namespace QuizApp\Middleware;

/*
 * Middleware to check if a user has already completed a survey
 */
class UserCompletedSurvey
{
    public function __invoke($request, $response, $next)
    {
         // Need to access $app->UserServices 
    } 
}

But how do I access the container from within the class? Do I have to register this Middleware as a service and inject into the constructor?

  1. 1 Posted by Rob Allen on 24 Nov, 2015 10:44 AM

    Rob Allen's Avatar

    Hi James,

    Yes. Inject via the DIC.

    Regards,

    Rob...

  2. 2 Posted by jameslovescode on 24 Nov, 2015 06:37 PM

    jameslovescode's Avatar

    Hi Rob,

    Out of interest are there any patterns that can help with managing dependencies? My index.php file is a long list of:

    $app['SomeService'] = function() {}
    

    Of course, I could put them in a separate file, but I'm looking for a more concrete solution to managing dependencies.

    Any ideas?

  3. 3 Posted by Rob Allen on 24 Nov, 2015 07:04 PM

    Rob Allen's Avatar

    Pimple isn't very clever, so I just put them all in dependencies.php.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac