如何使用 SessionServiceProvider 创建会话?
How to create a session with SessionServiceProvider?
我没有发现我的错误。我无法访问会话变量。
我试试这个:
$app = new Silex\Application();
$app['debug']=true;
$app->register(new Silex\Provider\SessionServiceProvider());
$app->get("/", function(Request $request) use($app)
{
return $app["session"]->get('is_admin');
});
$app->run();
use Symfony\Component\HttpFoundation\Request;
$app->before(function (Request $request, Silex\Application $app){
$app['session']->set('is_admin', true);
});
我这样做是为了解决问题(谢谢 Stepashka):
$app = new Silex\Application();
$app['debug']=true;
use Silex\Provider\SessionServiceProvider;
$app->register(new SessionServiceProvider());
$app->before(function() use ($app){
$app['session']->start();
$app['session']->set('is_admin', "dfghjdf");
});
$app->get("/", function() use($app)
{
return print_r($app['session']->get('is_admin'));
});
$app->run();
问题是您的提供程序在您 $app->run()
之前未初始化。您应该在控制器中或至少在 $app->run()
.
之后分配值
Silex 中还有一个 Middleware 功能,因此您可以这样完成作业:
$app->before(function (Request $request, Application $app) {
$app['session']->set('is_admin', true);
});
我没有发现我的错误。我无法访问会话变量。 我试试这个:
$app = new Silex\Application();
$app['debug']=true;
$app->register(new Silex\Provider\SessionServiceProvider());
$app->get("/", function(Request $request) use($app)
{
return $app["session"]->get('is_admin');
});
$app->run();
use Symfony\Component\HttpFoundation\Request;
$app->before(function (Request $request, Silex\Application $app){
$app['session']->set('is_admin', true);
});
我这样做是为了解决问题(谢谢 Stepashka):
$app = new Silex\Application();
$app['debug']=true;
use Silex\Provider\SessionServiceProvider;
$app->register(new SessionServiceProvider());
$app->before(function() use ($app){
$app['session']->start();
$app['session']->set('is_admin', "dfghjdf");
});
$app->get("/", function() use($app)
{
return print_r($app['session']->get('is_admin'));
});
$app->run();
问题是您的提供程序在您 $app->run()
之前未初始化。您应该在控制器中或至少在 $app->run()
.
Silex 中还有一个 Middleware 功能,因此您可以这样完成作业:
$app->before(function (Request $request, Application $app) {
$app['session']->set('is_admin', true);
});