【发布时间】:2019-01-08 13:36:53
【问题描述】:
我正在使用 PhalconPHP 并在登录后将值存储在会话中,如下所示。
$this->session->start();
$this->session->set('auth', array(
'dealer_id' => $dealers->getDealerId(),
'username' => $dealers->getUserName(),
'language_id' => $dealers->getLanguageId(),
'dealername' => $dealername,
));
session_write_close();
在会话中设置值,即使在我尝试打印之后也是如此
print_r($this->session->get('auth'))
返回
Array
(
[dealer_id] => 78
[username] => swiftmailcomm
[language_id] => 1
[dealername] => Swiftmail Communication
)
但是,当我尝试在 ControllerBase 中使用 $this->session->get('auth') 在某些操作中获取此会话值时,它不会返回任何内容。它似乎被摧毁了。
模块.php
public function registerServices(\Phalcon\DiInterface $di)
{
$di->set('dispatcher',function(){
$eventsManager = new EventsManager;
/**
* Check if the user is allowed to access certain action using the SecurityPlugin
*/
$eventsManager->attach('dispatch:beforeDispatch', new SecurityPlugin);
/**
* Handle exceptions and not-found exceptions using NotFoundPlugin
*/
$eventsManager->attach('dispatch:beforeException', new NotFoundPlugin);
$dispatcher = new MvcDispatcher;
$dispatcher->setEventsManager($eventsManager);
$dispatcher = new \Phalcon\Mvc\Dispatcher();
$dispatcher->setDefaultNamespace("NBBD_SkyWebTech\Dealer\Controllers\\");
return $dispatcher;
});
/**
* Read configuration
*/
$config = include __DIR__ . "/config/config.php";
//Register Volt as a service
$di['view'] = function () use ($config) {
$view = new View();
$view->setViewsDir($config->application->viewsDir);
//activating volt engine
$view->registerEngines(array(
".volt" => 'voltService'
));
return $view;
};
$di->set('voltService', function($view, $di) use ($config) {
$volt = new Volt($view, $di);
$volt->setOptions(array(
"compiledPath" => $config->application->voltCacheDir,
'compiledSeparator' => '_',
"compiledExtension" => ".compiled"
));
return $volt;
});
/*
* Setting up the view component
$di['view'] = function () use ($config) {
$view = new View();
$view->setViewsDir($config->application->viewsDir);
//activating volt engine
$view->registerEngines(array(
".phtml" => 'voltService'
));
return $view;
}; */
//Set the views cache service
$di->set('viewCache', function() use ($config) {
// Cache the files for 1hour using a Output frontend
$frontCache = new Output(array(
"lifetime" => 3600
));
//Cache data for one day by default
$cache = new File($frontCache, array(
"cacheDir" => $config->application->cacheDir
));
return $cache;
});
//Model Cache
$di->set('modelsCache', function() use ($config) {
//Cache data for one day by default
$frontCache = new \Phalcon\Cache\Frontend\Data(array(
"lifetime" => 3600
));
//Cache data for one day by default
$cache = new File($frontCache, array(
"cacheDir" => $config->application->cacheDir
));
return $cache;
});
//Set up the flash service
$di->set('flash', function() {
return new \Phalcon\Flash\Session(array(
'error' => 'alert alert-danger',
'success' => 'alert alert-success',
'notice' => 'alert alert-info',
));
});
/**
* Start the session the first time some component request the session service
*/
$di->set('session', function () {
$session = new SessionAdapter();
$session->start();
return $session;
});
}
【问题讨论】:
-
能否请您包括会话声明
-
我现在没有时间给你一个完整的答案,但你的问题是
session_write_close()。删除这些电话。如果它解决了问题,我将发布详细答案以供将来参考 -
@UlugToprak 我做了,但仍然是同样的问题.. 请发布完整的答案。谢谢
-
你不需要再调用
$this->session->start();,start()是在DI取回服务的时候调用的。 -
@UlugToprak 我已将其从代码中删除。问题依然存在...