Laravel Rewrite user Login Simple example _php instance

Source: Internet
Author: User
Tags comments smarty template

The example in this article describes how Laravel overrides user logins. Share to everyone for your reference, specific as follows:

Class Authcontroller extends Controller {//use throttleslogins, authenticatesandregistersusers;
  protected $redirectTo = ' admin/index ';
  protected $loginView = ' admin/login ';
  Protected $guard = ' admin ';
  protected $redirectAfterLogout = ' admin/login '; protected $maxLoginAttempts = 5; Maximum attempted logons per minute protected $lockoutTime = 600;
  Login Lockout time function __construct () {$this->middleware (' guest:admin ', [' except ' => ' logout ']); } Protected function Validator (array $data) {return Validator::make ($data, [' username ' => ' Required|max 
    : 255 ', ' email ' => ' required|email|max:255|unique:admin_users ', ' Password ' => ' Required|confirmed|min:6 ',
  ]); /** * @param request $request/protected function Validatelogin (Request $request) {$this->validate ($request, [$this->loginusername () => ' required ', ' password ' => ' required ', ' captcha ' => ' re Quired|captcha '], [' email.required '=> ' mailbox must ', ' password.required ' => ' password must ', ' Captcha.captcha ' => ' Authentication code error ', ' captcha.required ' =&gt ;
  ' Verification code must ',];
   /** * Rewrite login * @param Request $request * @return \illuminate\http\redirectresponse|\illuminate\http\response
    * * Public Function login (Request $request) {$this->validatelogin ($request); If the class is using the throttleslogins trait, we can automatically throttle//The login attempts to this appli cation.
    We ' ll key this by the username and//The IP address of the client making these requests to this application.
    $throttles = $this->isusingthrottlesloginstrait ();
    DD ($this->hastoomanyloginattempts ($request)); if ($throttles && $lockedOut = $this->hastoomanyloginattempts ($request)) {$this->firelockoutevent ($re
      Quest); Log records $this->login_logs ([' Email ' => $request->input (' email '), ' login_ip ' => $request->ip (), ' Login_ Result ' =>0, ' comments ' => ' Limit login 10 minutes ']);
    return $this->sendlockoutresponse ($request);
    $credentials = $this->getcredentials ($request); if (Auth::guard ($this->getguard ())->attempt ($credentials, $request->has (' remember ')) {//log record $this- >login_logs (' email ' => $request->input (' email '), ' login_ip ' => $request->ip (), ' Login_result ' =>1,
      ' Comments ' => ' login successful ']);
    return $this->handleuserwasauthenticated ($request, $throttles); //If The login attempt was unsuccessful we'll increment the number of attempts//to login and redirect the U Ser back to the login form.
    Of course, when this//user surpasses their maximum number of attempts they'll get locked out. if ($throttles &&! $lockedOut) {//Log records $this->login_logs ([' Email ' => $request->input (' email ')
      , ' login_ip ' => $request->ip (), ' Login_result ' =>0, ' comments ' => ' login failed ']);
    $this->incrementloginattempts ($request); } return $this->sendfailedloginresponse ($request);
  /** * Login Record * @param $data/Private Function Login_logs ($data) {loginlog::create ($data);

 }
}

Directly rewrite the login method, I actually copied the original method and added something of my own.

Some of the major changes are:

1. Add Verification Code (custom validation information and prompts).

2. Background login frequency limit.

3. Log in logging.

More interested in laravel related content readers can view the site topics: "Laravel Framework Introduction and Advanced Course", "PHP Excellent Development Framework Summary", "Smarty Template Primer Tutorial", "PHP date and Time usage summary", "PHP object-oriented Program Design Introductory Course ", PHP string (String) Usage summary," PHP+MYSQL Database operation Introduction Tutorial "and" PHP common database Operation Skills Summary "

I hope this article will help you with the PHP program design based on Laravel framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.