久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使 $_SESSION 在控制器中可用

Making $_SESSION available in controllers(使 $_SESSION 在控制器中可用)
本文介紹了使 $_SESSION 在控制器中可用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試使 $_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();
    }
} 

我的問題是如何使 $_SESSIONCoreController 中可用,以及在關(guān)閉序列期間它何時可用?我試過在 CoreControllerTemplate::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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Action View Helper in Zend - Work around?(Zend 中的動作視圖助手 - 解決方法?)
Is this a good way to match URI to class/method in PHP for MVC(這是將 URI 與 PHP 中用于 MVC 的類/方法匹配的好方法嗎)
Where do I save partial (views) in Zend Framework, to be accessible for all Views in my App?(我在哪里保存 Zend Framework 中的部分(視圖),以便我的應(yīng)用程序中的所有視圖都可以訪問?) - IT屋-程序員軟件開發(fā)技術(shù)
Having a single entry point to a website. Bad? Good? Non-issue?(有一個網(wǎng)站的單一入口點.壞的?好的?沒問題?)
Is MVC + Service Layer common in zend or PHP?(MVC + 服務(wù)層在 Zend 或 PHP 中常見嗎?)
Hello World example in MVC approach to PHP(PHP MVC 方法中的 Hello World 示例)
主站蜘蛛池模板: 久久久精品一区二区 | 日本免费视频 | 欧美亚洲国产一区二区三区 | 欧美亚洲视频在线观看 | 成人在线免费看 | 亚洲一区二区在线播放 | 午夜影院网站 | 国产特级毛片 | 欧美日韩电影在线 | 国产一区二区三区四区在线观看 | 亚洲 欧美 日韩 精品 | 欧美一区二区三区日韩 | 97久久久久久 | 在线一区| 亚洲精品中文字幕在线观看 | 在线黄av| 国产一区二区三区四区 | 国产无套一区二区三区久久 | 欧美日韩在线一区二区三区 | 在线观看日本高清二区 | 精品无码久久久久久国产 | jlzzjlzz欧美大全 | 欧美视频1 | 在线视频h | 2020国产在线| 色频| 亚洲成人免费在线观看 | 亚洲综合色丁香婷婷六月图片 | 欧美手机在线 | 久久视频精品 | 欧美日韩高清 | 亚洲 成人 在线 | 手机av网| 黄色毛片在线看 | 日本欧美大片 | 国产高清在线观看 | 国产午夜精品久久久久 | 亚洲精品欧美 | 成人精品视频 | h视频免费看 | 国产91网站在线观看 |