考虑以下内容:
namespace App'ART'Domain'Services;
use App'ART'Domain'Entities'User;
use Illuminate'Support'Facades'Auth;
/**
* Handle Sessions.
*/
class SessionService {
/**
* Create a new session based on user id.
*
* @param User $user
*/
public function createSession(User $user) {
Auth::loginUsingId($user->id);
}
/**
* Destroy a session if it exists.
*/
public function destroySession() {
if (Auth::check()) {
Auth::logout();
}
}
}
非常基本,这里没有什么花哨的。根据指定的用户id创建一个新的会话。
现在我得到:
ErrorException in Guard.php line 430:
Argument 1 passed to Illuminate'Auth'Guard::login() must be an instance of Illuminate'Contracts'Auth'Authenticatable, instance of App'ART'Domain'Models'User given, called in /var/www/AppResponseTracker/vendor/laravel/framework/src/Illuminate/Auth/Guard.php on line 489 and defined
堆栈跟踪的一部分:
in Guard.php line 430
at HandleExceptions->handleError('4096', 'Argument 1 passed to Illuminate'Auth'Guard::login() must be an instance of Illuminate'Contracts'Auth'Authenticatable, instance of App'ART'Domain'Models'User given, called in /var/www/AppResponseTracker/vendor/laravel/framework/src/Illuminate/Auth/Guard.php on line 489 and defined', '/var/www/AppResponseTracker/vendor/laravel/framework/src/Illuminate/Auth/Guard.php', '430', array()) in Guard.php line 430
at Guard->login(object(User), false) in Guard.php line 489
at Guard->loginUsingId('1')
at call_user_func_array(array(object(Guard), 'loginUsingId'), array('1')) in Manager.php line 137
at Manager->__call('loginUsingId', array('1')) in Facade.php line 210
at AuthManager->loginUsingId('1') in Facade.php line 210
at Facade::__callStatic('loginUsingId', array('1')) in SessionService.php line 19
at Auth::loginUsingId('1') in SessionService.php line 19
最后一行:at Auth::loginUsingId('1') in SessionService.php line 19
他们是否更改了阻止我传入ID的东西?
更新! !
我有其他代码,说明如果用户登录在Auth::check()
,然后允许访问路由,如果没有。抛出此错误后,授予访问权限…
这似乎不对…
很抱歉之前的评论,我没有理解你的问题。
,因为loginUsingId()函数正在设置会话,然后调用login()函数。
public function loginUsingId($id, $remember = false){
$this->session->set($this->getName(), $id); $this->login($user = $this->provider->retrieveById($id), $remember); return $user; }
和login()函数接受一个Authenticatable类型的用户对象
public function login(Authenticatable $user, $remember = false);
所以你的模型应该实现AuthenticatableContract
class User extends Model implements AuthenticatableContract{//Code goes here}