Laravel 静态验证 class "Non-static method" 错误
Laravel Auth in static class "Non-static method" error
我正在制作 class 在一个文件中处理无聊的身份验证内容。
但是我在调用 auth 函数时遇到了小问题。
我遇到的第一个问题是这个函数:
hasTooManyLoginAttempts
代码
if ($this->hasTooManyLoginAttempts($request)) {
触发错误:
Using $this when not in object context
当我将 $this-> 更改为 self::
if (self::hasTooManyLoginAttempts($request)) {
触发器
Non-static method My\AuthClass::hasTooManyLoginAttempts() should not be called statically
示例class我正在尝试使用
namespace My\AuthClass;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
class AuthClass
{
use AuthenticatesUsers;
public static function doLogin(Request $request)
{
// Validate login
// Has too many login attempts?
if (self::hasTooManyLoginAttempts($request)) {
self::fireLockoutEvent($request);
return redirect()->route('login.show')->with('error', 'You have tried to login too many times in short amount of time. Please try again later.');
}
// Try authenticate
// Send login error
}
}
感谢帮助!
public static function doLogin(Request $request)
显然是静态函数,而 hasTooManyLoginAttempts
不是。
您不能使用双冒号调用它,也不能在静态上下文中使用 $this
。好尴尬。
您必须创建一种解决方法:
class AuthClass
{
use AuthenticatesUsers;
private static $instance = null;
public static function doLogin(Request $request)
{
// Validate login
$self = self::init();
// Has too many login attempts?
if ($self->hasTooManyLoginAttempts($request)) {
$self->fireLockoutEvent($request);
return redirect()->route('login.show')->with('error', 'You have tried to login too many times in short amount of time. Please try again later.');
}
// Try authenticate
// Send login error
}
public static function init() {
if(null === self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
}
这里的重要部分是新的 init()
函数(您可以随意命名)。它将创建当前 class 的新实例,并允许您在 "static" 上下文中使用 ->
(它不是真正静态的)。
正如用户 Tuim 在评论中指出的那样,外观也可以,但 "trick" 大致相同。
我正在制作 class 在一个文件中处理无聊的身份验证内容。
但是我在调用 auth 函数时遇到了小问题。
我遇到的第一个问题是这个函数:
hasTooManyLoginAttempts
代码
if ($this->hasTooManyLoginAttempts($request)) {
触发错误:
Using $this when not in object context
当我将 $this-> 更改为 self::
if (self::hasTooManyLoginAttempts($request)) {
触发器
Non-static method My\AuthClass::hasTooManyLoginAttempts() should not be called statically
示例class我正在尝试使用
namespace My\AuthClass;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
class AuthClass
{
use AuthenticatesUsers;
public static function doLogin(Request $request)
{
// Validate login
// Has too many login attempts?
if (self::hasTooManyLoginAttempts($request)) {
self::fireLockoutEvent($request);
return redirect()->route('login.show')->with('error', 'You have tried to login too many times in short amount of time. Please try again later.');
}
// Try authenticate
// Send login error
}
}
感谢帮助!
public static function doLogin(Request $request)
显然是静态函数,而 hasTooManyLoginAttempts
不是。
您不能使用双冒号调用它,也不能在静态上下文中使用 $this
。好尴尬。
您必须创建一种解决方法:
class AuthClass
{
use AuthenticatesUsers;
private static $instance = null;
public static function doLogin(Request $request)
{
// Validate login
$self = self::init();
// Has too many login attempts?
if ($self->hasTooManyLoginAttempts($request)) {
$self->fireLockoutEvent($request);
return redirect()->route('login.show')->with('error', 'You have tried to login too many times in short amount of time. Please try again later.');
}
// Try authenticate
// Send login error
}
public static function init() {
if(null === self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
}
这里的重要部分是新的 init()
函数(您可以随意命名)。它将创建当前 class 的新实例,并允许您在 "static" 上下文中使用 ->
(它不是真正静态的)。
正如用户 Tuim 在评论中指出的那样,外观也可以,但 "trick" 大致相同。