php会话闪存消息 [英] php session flash message

查看:132
本文介绍了php会话闪存消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在重定向后创建会话flash消息。



我有Controller类

  class Controller 
{
function __construct()
{
if(!empty($ _ SESSION ['FLASH']))
foreach($ _ SESSION ['FLASH'] as $ key => $ val)
$ this-> $ key = $ val;
}
function __destruct()
{
$ _SESSION ['FLASH'] = null;
}
}

也有Controller子类Home,如/ Home / Index => public function index()

  class Home extend Controller 
{
function __construct()
{
parent :: __ construct();
}

public function index()
{
//其中我想显示$ this->消息一次
echo $ this- >消息; // but $ this-> message is undefinded why?
}
public function Post_register(){
// post post数据后
//验证

//此函数重定向到/ Home / Index above function index();
Uri :: redirectToAction(Home,Index,array('message'=>'some message'));
}
}

和uri类函数。 p>

  public static function redirectToAction($ controller,$ method,$ arr)
{
$ _SESSION ['FLASH '] = $ arr;
header(Location:/。$ controller。'/'。$ method);
}

$ this-> message 未定义为什么?

解决方案

我为这种类型的项目写了一个库 https://github.com/tamtamchik/simple-flash





redirectToAction

  public static function redirectToAction($ controller,$ method,$ arr)
{
flash($ arr ['message']) ;
header(Location:/。$ controller。'/'。$ method);
}

  public function index()
{
echo flash
}

它会生成 Bootstrap 友好的提醒讯息。


i'm tryning to create session flash message after redirection.

i have Controller class

class Controller
{
    function __construct()
    {
    if(!empty($_SESSION['FLASH']))
        foreach($_SESSION['FLASH'] as $key => $val)
        $this->$key = $val;
    }
    function __destruct()
    {
        $_SESSION['FLASH']=null;
    }
}

also i have Controller child class Home, where functions are run by route, like /Home/Index => public function index()

class Home extends Controller
{
    function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        //where i want to display $this->message only once
        echo $this->message; // but $this->message is undefinded why? 
    }
    public function Post_register(){
        //after post form data
        // validation 

        // this function redirect to /Home/Index  above function index();
        Uri::redirectToAction("Home","Index",array('message' => 'some message'));
    }
}

and uri class function where i redirecting user.

public static function redirectToAction($controller,$method,$arr)
{
    $_SESSION['FLASH'] = $arr;
    header("Location:/".$controller.'/'.$method);
}

but $this->message is undefinded why?

解决方案

I wrote a library just for this type of projects https://github.com/tamtamchik/simple-flash.

Once you have it installed you can do this.

In your redirectToAction:

public static function redirectToAction($controller,$method,$arr)
{
    flash($arr['message']);
    header("Location:/".$controller.'/'.$method);
}

And in index:

public function index()
{
    echo flash()->display(); 
}

It'll generate Bootstrap friendly alert messages.

这篇关于php会话闪存消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆