简体   繁体   English

如何将变量从preDispatch传递给控制器​​和动作?

[英]how to pass a variable from preDispatch to controller and action?

I try to put a variable in preDispatch function that check login, and I want to pass this variable to controllers (for Action to use). 我尝试在preDispatch函数中放置一个变量来检查登录,并且我想将此变量传递给控制器​​(供Action使用)。

I just try $this->getRequest()->setParam('check_login', 'FALSE'); 我只是尝试$this->getRequest()->setParam('check_login', 'FALSE');

But it doesn't work ... 但这不起作用...

Do anyone know how to do that? 有人知道该怎么做吗?

if you have no particular reason to pass the variable in the request object assign that variable to a private or protected member: 如果没有特殊原因在请求对象中传递变量,则将该变量分配给私有成员或受保护成员:

<?php
class my_Controller extends blah blah
{
    protected $login; 

    public function preDispatch()
    {
        $this->login = what it needs to be
    }
}

However the code you present works for me. 但是,您提供的代码对我有用。 Except for one small item. 除了一件小东西。 If you wish to use 'FALSE' as a boolean you may need to remove the quotes. 如果您希望将“ FALSE”用作布尔值,则可能需要删除引号。 The way it is presented at the moment 'FALSE' is a string and may not work properly as a boolean operator. 目前以'FALSE'表示的方式是字符串,可能无法作为布尔运算符正常工作。

$this->getRequest()->setParam('check_login', FALSE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM