5

我是PHPLaravel的新手,遇到以下问题。

我知道 Laravel 提供了一个现成的登录系统,使用以下语句创建:

php artisan make:auth

问题是这个系统直接与数据库交互。

我的情况不同,因为我的 Laravel 应用程序只实现了前端。所有业务逻辑都由公开 REST Web 服务的Java 后端应用程序处理。

基本上 Laravel 前端应用程序是这样的:

1)显示登录表单(用户名和密码)的视图。

2) 一个控制器类,它包含一个方法,该方法接收来自先前表单的提交,然后调用后端应用程序的 REST Web 服务(发送一个请求,该请求具有包含插入的用户名和密码的授权标头)。

后端应用程序将返回(到前面的 Laravel 控制器方法中)一个包含用户信息的JSON对象作为响应,就像这样(如果用户被授权)......

{
  "userName": "Painkiller",
  "email": "painkiller@gmail.com",
  "enabled": true
}

...或者,在用户未获得授权的情况下,类似这样...

{
  "timestamp": 1485183649134,
  "status": 401,
  "error": "Unauthorized",
  "message": "Credenziali non valide",
  "path": "/Extranet/login"
}

我最初的想法是编写一个执行这些操作的自定义控制器:

  1. 控制器处理登录表单提交(包含用户输入的凭据)。
  2. 控制器调用我的后端 Web 服务并获取 JSON 对象,如果 JSON 对象代表授权用户,则将其转换为代表用户的 PHP 模型对象。
  3. 控制器将此模型对象放入会话中并重定向到可以从会话中检索此信息的下一个用户页面。

我不太喜欢前端开发,但我认为它应该可以工作,但是......我正在远离 Laravel 架构和 Laravel 逻辑。

所以,我的 Laravel 应用程序不能直接与数据库对话,但我想也许我可以尝试采用 Laravel 架构。

所以我在想,在我的 Laravel 项目中,默认情况下我有一个代表标准 Laravel 登录系统的\app\Http\Controllers\Auth\LoginController类,这个:

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,这条线到底是做什么的?起初,我认为它在LoginController中添加了AuthenticatesUsers功能,但它更多的是与扩展和继承概念相关的行为。

无论如何, 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对象,将这些信息放入会话并重定向到用户页面吗?

这对我来说是一个聪明的解决方案吗?

4

1 回答 1

3

您不必这样做,您只需制作路线并覆盖登录方法或创建自己的方法。

路线:

// 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
    ]);

}
于 2018-03-13T10:00:08.243 回答