我是 PHP 和 Laravel 的新手,我遇到以下问题。
我知道Laravel使用以下语句提供了一个非常完善的自动登录系统创建:
php artisan make:auth
问题是该系统直接与数据库交互。
我的情况不同,因为我的Laravel应用程序只实现了前端。所有业务逻辑都由 Java后端应用程序公开REST Web服务来处理。
基本上Laravel前端应用程序是这样的:
1)查看,显示登录表单(用户名和密码)。
2)一个控制器类,它包含一个方法,该方法接收上一个表单的提交并调用后端应用程序的REST Web服务(使用包含插入的用户名的授权头进行requet和密码。
后端应用程序将返回(进入上一个Laravel控制器方法) JSON ,其中包含用户信息,如repsonse(如果用户已获得授权):
{
"userName": "Painkiller",
"email": "painkiller@gmail.com",
"enabled": true
}
或者,如果用户未获得授权,请执行以下操作:
{
"timestamp": 1485183649134,
"status": 401,
"error": "Unauthorized",
"message": "Credenziali non valide",
"path": "/Extranet/login"
}
我最初的想法是编写一个执行这些操作的自定义控制器:
我不是很喜欢前端开发,但我认为它应该可行但是...我正在远离Laravel架构和Laravel逻辑。
因此,我的Laravel应用程序无法直接与数据库通信,但我想也许我可以尝试采用Laravel架构。
所以我认为在我的Laravel项目中默认情况下我有 \ app \ Http \ Controllers \ Auth \ LoginController 类代表标准的Laravel登录系统,这个:
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
}
它包含以下行:
use AuthenticatesUsers;
我不是很喜欢PHP,这行究竟是什么?我第一次认为它将 AuthenticatesUsers 功能添加到 LoginController ,但它更像是与 extends 和继承概念相关的行为。
无论如何,似乎 AuthenticatesUsers 类包含处理登录的逻辑的实现,这个方法:
public function login(Request $request)
{
$this->validateLogin($request);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if ($this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
$this->incrementLoginAttempts($request);
return $this->sendFailedLoginResponse($request);
}
所以我在想:我可以覆盖此方法来执行对我的Web服务的调用,获取与已登录用户相关的JSON,将这些信息放入会话并重定向到用户页面吗?
可以成为我的蠢货的智能解决方案吗?
答案 0 :(得分:2)
您不必这样做,您只需制作路线并覆盖登录方法或创建自己的路线。
路线:
// authentication routes
Route::get('/login', 'Auth\LoginController@showLoginForm')->name('adminGetLogin');
Route::post('/login', 'Auth\LoginController@login')->name('adminPostLogin');
Route::get('/logout', 'Auth\LoginController@logout')->name('adminLogout');
// reset password routes
Route::get('/password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('showResetForm');
Route::post('/password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('sendResetEmail');
Route::get('/password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('showResetForm');
Route::post('/password/reset', 'Auth\ResetPasswordController@reset')->name('resetPassword');
网络
public function login(Request $request){
if (! Auth::attempt(['email' => $request->email, 'password' => $request->password, 'is_active' => 1])) {
// do whatever yo desire
return redirect('/home');
}
REST API
public function login(Request $request){
if (Auth::attempt(['email' => $request->email, 'password' => $request->password, 'is_active' => 1])) {
// do whatever yo desire
$user = Auth::user();
return response()->json([
'Error' => ['type' => 'success', 'desc' => 'Login successful', 'code' => 0],
'Response' => $user
]);
}