handsome
handsome

Reputation: 2412

Slim PHP access object in route group

hello I want to create an object that can be used inside all the nested routes

use Slim\App;
use Slim\Http\Request;
use Slim\Http\Response;

return function (App $app) {

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

        $this->user = \User::findOrFail(1);

        $app->get('/profile', function ($request, $response, $args) {
            var_dump($this->user);
        });

    });
};

the error I´m getting is

Type: Slim\Exception\ContainerValueNotFoundException
Message: Identifier "user" is not defined.

Upvotes: 0

Views: 285

Answers (1)

stillKonfuzed
stillKonfuzed

Reputation: 412

for that you should use a container

$app = new \Slim\App();
$container = $app->getContainer();
$container['user'] = function () {
    //code
};

Upvotes: 2

Related Questions