我有一个我在Phalcon项目中创建的课程。我想从session
获取DI
服务,以便检查是否设置了变量。
我试过了:
$di = Phalcon\DI::getDefault();
echo $di->get("session")->get("user_id");
exit;
然而它回来说A session had already been started - ignoring session_start()
use Phalcon\Mvc\Model\Resultset\Simple, Phalcon\Mvc\User\Plugin;
class JsonReturnObject extends Plugin
我的会话服务注册如下:
<?php
use Phalcon\Mvc\View;
use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\Url as UrlProvider;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Metadata\Memory as MetaData;
use Phalcon\Session\Adapter\Files as SessionAdapter;
use Phalcon\Flash\Session as FlashSession;
use Phalcon\Events\Manager as EventsManager;
//setup the dependency injection
$di = new FactoryDefault();
//setup the session
$di->set('session', function() {
$session = new SessionAdapter();
$session->start();
return $session;
});
任何帮助都将不胜感激。
答案 0 :(得分:2)
尝试将会话注册为不是这样的共享组件:
$di->set(
'session',
function () {
$session = new \Phlacon\Session\Adapter\Files();
$session->start();
return $session;
}
);
请注意,true
$di->set()
或者使用$di->set()
代替$di->setShared()
答案 1 :(得分:0)
修复方法是使用setShared
将服务设置为共享。