問題描述
我正在嘗試使 $_SESSION
全局在我從頭編寫的框架的控制器中可用.不完全是MVC,表示層由兩個父類和多個子類組成.
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.
沒有詳細說明,我的視圖是在 class Template
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');
}
}
}
然后,在構(gòu)造函數(shù)中實例化 class Template
之后,我在父控制器中的析構(gòu)函數(shù)中調(diào)用 Template::render()
.所有類都在自動加載.
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();
}
}
我的問題是如何使 $_SESSION
在 CoreController
中可用,以及在關(guān)閉序列期間它何時可用?我試過在 CoreController
和 Template::render()
中直接調(diào)用它,總是得到未定義的變量警告,但是定義了 $_SESSION
在我看來作品.這背后的原因是我想根據(jù)是否設(shè)置會話 id 來設(shè)置某些變量,并且我想將大多數(shù)演示邏輯保留在我的控制器中.提前致謝.
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 是一種存儲形式.也就是說,它只能在模型層的深處使用.
Session is a form of storage. Which means, that it should only be used deep within model layer.
在表示層操作 $_SESSION
與在控制器和/或視圖中編寫 SQL 相當(dāng).您將消除 SoC 的最后痕跡……盡管您已經(jīng)在實現(xiàn) Rails 之類的ViewController"怪物.
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.
與其在表示層泄漏存儲邏輯,不如使用類似的映射器,如用于 sql.
Instead of leaking your storage logic in the presentation layer, you should be using similar mappers like for the 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;
}
控制器只與服務(wù)交互
public function postLogin( $request )
{
$auth = $this->serviceFactory->create('recognition');
$auth->identify([
'login' => $request->getParameter('username'),
'pass' => $request->getParameter('password'),
]);
}
服務(wù)工廠將被注入到控制器(以及伴隨的視圖)的構(gòu)造函數(shù)中.
The service factory would be injected in the controller's (and the accompanying view's) constructor.
注意:以上代碼只是為了說明這一點,不應(yīng)復(fù)制粘貼或以其他方式嫁接到生產(chǎn)代碼上.
Note: the code above is only to illustrate the point and should not be copy-pasted or otherwise grafted on a production code.
這篇關(guān)于使 $_SESSION 在控制器中可用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!