你好,我想创建一个可以在所有嵌套路由内使用的对象
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);
});
});
};
我得到的错误是
Type: Slim\Exception\ContainerValueNotFoundException
Message: Identifier "user" is not defined.
答案 0 :(得分:1)
为此,您应该使用容器
$app = new \Slim\App();
$container = $app->getContainer();
$container['user'] = function () {
//code
};