使$ _SESSION在控制器中可用 [英] Making $_SESSION available in controllers
问题描述
我正在尝试使$_SESSION
全局在我的框架控制器中完全可用,这些都是从头开始编写的.它不是MVC,表示层由两个父类和多个子类组成.
我的视图在class Template
class Template{
protected $_controller;
protected $_action;
function __construct($controller,$action) {
$this->_controller = $controller;
$this->_action = $action;
}
function render(){
if (file_exists(APP_ROOT . DS . 'app' . DS . 'view' . DS . $this->_controller . DS . $this->_action . '.php')) {
include (APP_ROOT . DS . 'app' . DS . 'view' . DS . $this->_controller . DS . $this->_action . '.php');
}
}
}
然后在实例化构造函数中的class Template
之后,在父控制器的析构函数中调用Template::render()
.所有类都将自动加载.
class CoreController {
protected $_controller;
protected $_action;
protected $_template;
function __construct($controller, $action) {
$this->_controller = ucfirst($controller);
$this->_action = $action;
$this->_template = new Template($controller,$action);
}
function __destruct() {
$this->_template->render();
}
}
我的问题是如何在CoreController
中使$_SESSION
可用,以及在关闭顺序期间何时确切可用?我尝试直接在CoreController
和Template::render()
中调用它,并且总是收到未定义的变量警告,但是在我的视图中定义$_SESSION
是可行的.这背后的原因是我想根据是否设置了会话ID来设置某些变量,并且我想将大多数表示逻辑保留在控制器中.提前致谢.
会话是一种存储形式.这意味着,只能在模型层的深处使用它.
在表示层中操纵$_SESSION
与在控制器和/或视图中拧SQL相当.您将消除 SoC 的最后痕迹……尽管您已经在研究它了.像"ViewController"一样实现Rails怪异.
您应该在SQL中使用类似的映射器,而不是在表示层泄漏存储逻辑.
来自模型层
中的某些服务
public function identify( $parameters )
{
$user = $this->domainObjectFacctory->create('user');
$mapper = $this->mapperFactory->create('session');
if ( $mapper->fetch($user, 'uid') === false )
{
$mapper = $this->mapperFactory->create('user');
$user->setUsername($parameters['login']);
$user->setPassword($parameters['pass']);
$mapper->fetch($user);
}
$this->currentUser = $user->isValid()
? $user
: null;
}
控制器仅与服务交互
public function postLogin( $request )
{
$auth = $this->serviceFactory->create('recognition');
$auth->identify([
'login' => $request->getParameter('username'),
'pass' => $request->getParameter('password'),
]);
}
服务工厂将被注入到控制器(和伴随的视图)的构造函数中.
注意:上面的代码仅用于说明要点,不应复制粘贴或以其他方式嫁接到生产代码上.
I'm trying to make the $_SESSION
global available within the controllers of my framework written from scratch. It's not quite MVC, the presentation layer consists of two parent classes with multiple child classes.
Without going into great detail, my views are rendered in class Template
class Template{
protected $_controller;
protected $_action;
function __construct($controller,$action) {
$this->_controller = $controller;
$this->_action = $action;
}
function render(){
if (file_exists(APP_ROOT . DS . 'app' . DS . 'view' . DS . $this->_controller . DS . $this->_action . '.php')) {
include (APP_ROOT . DS . 'app' . DS . 'view' . DS . $this->_controller . DS . $this->_action . '.php');
}
}
}
Then I'm calling Template::render()
in a destructor within my parent controller after instantiating class Template
within a constructor. All classes are being autoloaded.
class CoreController {
protected $_controller;
protected $_action;
protected $_template;
function __construct($controller, $action) {
$this->_controller = ucfirst($controller);
$this->_action = $action;
$this->_template = new Template($controller,$action);
}
function __destruct() {
$this->_template->render();
}
}
My question is how can I make $_SESSION
available in CoreController
and when exactly is it available during the shutdown sequence? I've tried calling it directly in CoreController
as well as within Template::render()
and always get undefined variable warnings, however defining $_SESSION
within my views works. The reasoning behind this is I would like to set certain variables based off of whether or not the session id is set and I'd like to keep most presentation logic within my controllers. Thanks in advance.
Session is a form of storage. Which means, that it should only be used deep within model layer.
Manipulating $_SESSION
in presentation layer would be comparable with wring SQL in controllers and/or views. You would be eliminating the last vestiges of SoC ... though you have been already at it by implementing the Rails like "ViewController" monstrosity.
Instead of leaking your storage logic in the presentation layer, you should be using similar mappers like for the sql.
from some service in model layer
public function identify( $parameters )
{
$user = $this->domainObjectFacctory->create('user');
$mapper = $this->mapperFactory->create('session');
if ( $mapper->fetch($user, 'uid') === false )
{
$mapper = $this->mapperFactory->create('user');
$user->setUsername($parameters['login']);
$user->setPassword($parameters['pass']);
$mapper->fetch($user);
}
$this->currentUser = $user->isValid()
? $user
: null;
}
The controller only interacts with services
public function postLogin( $request )
{
$auth = $this->serviceFactory->create('recognition');
$auth->identify([
'login' => $request->getParameter('username'),
'pass' => $request->getParameter('password'),
]);
}
The service factory would be injected in the controller's (and the accompanying view's) constructor.
Note: the code above is only to illustrate the point and should not be copy-pasted or otherwise grafted on a production code.
这篇关于使$ _SESSION在控制器中可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!