使 $_SESSION 在控制器中可用

时间:2023-01-03
本文介绍了使 $_SESSION 在控制器中可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试使 $_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');
        }
    }

}

然后,在构造函数中实例化 class Template 之后,我在父控制器中的析构函数中调用 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 中可用,以及在关闭序列期间它何时可用?我试过在 CoreControllerTemplate::render() 中直接调用它,总是得到未定义的变量警告,但是定义了 $_SESSION 在我看来作品.这背后的原因是我想根据是否设置会话 id 来设置某些变量,并且我想将大多数演示逻辑保留在我的控制器中.提前致谢.

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 相当.您将消除 SoC 的最后痕迹……尽管您已经在实现 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;
}

控制器只与服务交互

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 在控制器中可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:将 URL 路由转换为函数参数 php mvc 下一篇:使用 codeigniter 实现动态菜单/页眉/页脚的最佳方法

相关文章

最新文章