我试图使用事件监听器为laravel上的所有成功登录创建一个Ip记录器,这是我的Login的监听器。
<?php
namespace App\Listeners;
use Illuminate\Auth\Events\Login;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class LogSuccessfulLogin
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct(Request $request)
{
$this->request = $request;
}
/**
* Handle the event.
*@author Rahul R
* @param Login $event
* @return void
*/
public function handle(Login $event)
{
$user = $event->user;
$user->last_login_at = date('Y-m-d H:i:s');
$user->last_login_ip=$this->request->ip();
$user-save();
}
}
这是我的loginController
namespace App\Http\Controllers\pages;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\UserLoginModel;
use App\User;
use Illuminate\Support\Facades\Auth;
use Zizaco\Entrust\EntrustFacade as Entrust;
class LoginController extends Controller
{
/**
* User Authentication Table - `users`
* @author Rahul
*
* @method POST
* @param Request | $request
* @return array | $error_message
* @return redirect | login | dashboard
*/
protected function auth(Request $request) {
$user = new User;
$user->email_address = $request->get('email_address');
$user->password = $request->get('password');
$userCredentials['email_address'] = $user->email_address;
$userCredentials['password'] = $user->password;
/* Making Authentication Request */
if(Auth::attempt($userCredentials))
{
/* If Auth true */
if (Entrust::hasRole('admin')) {
return redirect('admin/dashboard');
} else {
return redirect('dashboard');
}
} else {
/* If Auth false */
return redirect("login")->with('failed_login', 'Invalid email address or password.');
}
return redirect('login');
}
}
我在哪里做错了 我尝试将App \ Listeners \ Request添加到logincontroller但它会产生冲突
那么我需要获得成功登录的IP并将其写入数据库的最终结果是什么。
答案 0 :(得分:0)
在LogSuccessfulLogin类文件中添加以下语句:
use \Illuminate\Http\Request;
因为你在构造函数中使用了Request类,php认为它在App \ Listeners中。