search
HomeBackend DevelopmentPHP TutorialLaravel 5.5 implements front-end and back-end login

Laravel 5.5 implements front-end and back-end login

Dec 21, 2017 pm 02:16 PM
laravelaccomplishLogin

Use laravel5.5 to implement a function. This article mainly introduces to you the relevant information on how Laravel 5.5 implements front-end and back-end login based on the built-in Auth module. The article introduces it in detail through sample code. I hope it can help you.

The method is as follows:

Step 1: Generate Auth

After the project is established, generate the built-in Auth component:

php artisan make:auth

Step 2: Add guard

Open config\auth.php. In order to facilitate front-end and back-end user switching, the projects share the Users table

'guards' => [
 //...
 'admin' => [
  'driver' => 'session',
  'provider' => 'users',
 ],
 //...
 ],

Step 3: Implement the base class

class AdminController extends BaseController
{
 use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

 public function __construct()
 {
 $this->middleware('auth:admin');
 }
}

Step 4: Implement background login control Device

New appcontrollersAdminLoginController.php

<?php namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
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 = &#39;/admin&#39;;
 /**
 * Create a new controller instance.
 *
 * @return void
 */
 public function __construct()
 {
 $this->middleware('guest:admin')->except('logout');
 }
 /**
 * 重写登陆页面
 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
 */
 public function showLoginForm()
 {
 return view('backend.login');
 }
 /**
 * 重写退出方法
 * @param Request $request
 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
 */
 public function logout(Request $request)
 {
 $this->guard()->logout();
 $request->session()->flush();
 $request->session()->regenerate();
 return redirect('/admin/login');
 }
 /**
 * 重写guard认证
 * @return mixed
 */
 protected function guard()
 {
 return Auth::guard('admin');
 }
}

Step 5: Jump to different paths after login

app\Middleware\RedirectIfAuthenticated.php

public function handle($request, Closure $next, $guard = null)
{
 if (Auth::guard($guard)->check()) {
  $path = $guard ? '/admin' : '/home';
  return redirect($path);
 }
 return $next($request);
}

Steps 6: Implementation of jumping to different login pages if authentication fails

app\Exceptions\Handler.php

/**
 * 重写实现未认证用户跳转至相应登陆页
 * @param \Illuminate\Http\Request $request
 * @param AuthenticationException $exception
 * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
 */
 protected function unauthenticated($request, AuthenticationException $exception)
 {
 if($request->expectsJson()){
  return response()->json(['message' => $exception->getMessage()], 401);
 }else{
  return in_array('admin', $exception->guards()) ? return redirect()->guest('/admin/login') : redirect()->guest('login');
 }
 }

Related recommendations:

How to call front and backend methods in javascript Summary of examples

Package Auto Discovery in Laravel5.5 details introduction

Laravel5.5 new features error reporting and graphic introduction

The above is the detailed content of Laravel 5.5 implements front-end and back-end login. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

What is autoloading in PHP?What is autoloading in PHP?Apr 30, 2025 pm 03:37 PM

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

What are PHP streams?What are PHP streams?Apr 30, 2025 pm 03:36 PM

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

What is the maximum size of a file that can be uploaded using PHP ?What is the maximum size of a file that can be uploaded using PHP ?Apr 30, 2025 pm 03:35 PM

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

What is Nullable types in PHP ?What is Nullable types in PHP ?Apr 30, 2025 pm 03:34 PM

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

What is the difference between the unset() and unlink() functions ?What is the difference between the unset() and unlink() functions ?Apr 30, 2025 pm 03:33 PM

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools