idlatest
idlatest

Reputation: 43

accessing a container in route group in SlimPHP

I am new to SlimPHP framework. I have been trying to get a container inside route group and here is the error that shows up:

PHP Warning: Missing argument 2 for Slim\App::get()

Here is my code for routes:

$app->group('/api', function() use ($app) {

    $jwtMiddleware = $app->get('jwt');

    $this->post('/auth/signup', 'RegisterController:signup');
    $this->post('/auth/login', 'LoginController:login');

    //User routess
    $this->get('/user', 'UserController:getUser')->add($jwtMiddleware);
    //$this->put('/user');
});

and the code for my container:

// Jwt Middleware
$container['jwt'] = function ($container) {
    $jws_settings = $container->get('settings')['jwt'];
    return new \Slim\Middleware\JwtAuthentication($jws_settings);
};

please guys, what could be the problem?

Upvotes: 1

Views: 1112

Answers (1)

odan
odan

Reputation: 4952

You can get the container like this:

$app->group('/api', function(\Slim\App $app) {
    /* @var \Slim\App $this */
    $jwtMiddleware = $this->getContainer()->get('jwt');

    // ...
});

In the group callback $app and $this is already your Slim\App object. You don't need the use ($app).

Upvotes: 1

Related Questions