php - Laravel Auth和Session无法正常工作

时间:2014-03-30 04:16:01

标签: php session authentication laravel laravel-4

以下代码打印“test:bar”但不打印“home:bar”。此代码在我的XAMPP服务器上本地工作正常,但在我的webhost服务器上不起作用。起初我认为这是PHP版本的问题,但我不认为它已经存在了。我想它必须是我必须在webhost上启用的一些设置?有谁知道这可能导致什么?

class HomeController extends BaseController {

    public function home()
    {
        echo "home: " . Session::get('foo');
    }

    public function test()
    {
        Session::put('foo','bar');
        echo "test: " . Session::get('foo');
        return Redirect::route('home');
    }

}

Actual Output:
test: bar
home:

Expected Output:
test: bar
home: bar

1 个答案:

答案 0 :(得分:1)

Laravel希望路由(和控制器)的响应返回。由于您没有返回重定向,因此会话不会保留,并且重定向可能不会发生。只需更新您的方法即可返回正确的答案:

class HomeController extends BaseController {

    public function home()
    {
        return "home: " . Session::get('foo');
    }

    public function test()
    {
        Session::put('foo','bar');

        return Redirect::route('home');
    }

}

当你返回一个重定向时,没有任何回应,因为无论如何都不会看到响应。