Laravel 5.0 Session 不能使用静态方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27126972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.0 Session can't use static methods
提问by user13746
I'm trying to create app with Laravel 5.0 and I have problem with using Session methods in my BaseController constructor
我正在尝试使用 Laravel 5.0 创建应用程序,但在BaseController 构造函数中使用 Session 方法时遇到问题
Here is my BaseController with it's consturctor.
这是我的 BaseController 及其构造函数。
use Symfony\Component\HttpFoundation\Session\Session;
class BaseController extends Controller {
/**
* Initializer.
*
* @access public
* @return \BaseController
*/
public function __construct()
{
//set default lang
if (!Session::has('lang')) {
Session::put('lang', App::getLocale());
Cookie::forever('lang', App::getLocale());
} else {
App::setLocale(Session::get('lang'));
}
}
}
but I get this error
但我收到这个错误
ContextErrorException in BaseController.php line 22: Runtime Notice: Non-static method Symfony\Component\HttpFoundation\Session\Session::has() should not be called statically, assuming $this from incompatible context
BaseController.php 第 22 行中的 ContextErrorException:运行时注意:非静态方法 Symfony\Component\HttpFoundation\Session\Session::has() 不应静态调用,假设 $this 来自不兼容的上下文
Does anyone know what I'm doing wrong?
有谁知道我做错了什么?
回答by Marcin Nabia?ek
Instead of:
代替:
use Symfony\Component\HttpFoundation\Session\Session;
you should use:
你应该使用:
use Session;
to import Session here.
在这里导入会话。
回答by Joel Hinz
You're importing the wrong class. Symfony\Component\HttpFoundation\Session\Session
is the class used by Laravel when you call the Session::
facade - but they're not the same class, and Symfony's session class doesn't use a static method for has()
. I haven't used Laravel 5 myself, but I think it's in the top-level namespace.
你导入了错误的类。Symfony\Component\HttpFoundation\Session\Session
是 Laravel 在您调用Session::
外观时使用的类- 但它们不是同一个类,并且 Symfony 的会话类不使用静态方法has()
. 我自己没有使用过 Laravel 5,但我认为它在顶级命名空间中。