How to overridden laravel default login function?

by aniya.jaskolski , in category: PHP Frameworks , a month ago

How to overridden laravel default login function?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lottie , a month ago

@aniya.jaskolski 

To override the default login function in Laravel, you can create a new controller that extends Laravel's default Auth controller and then override the login method with your custom logic.


Here's a step-by-step guide on how to do this:

  1. Create a new controller that extends Laravel's default Auth controller. You can create this controller using the following command:
1
php artisan make:controller CustomAuthController


This will create a new controller file in the app/Http/Controllers directory.

  1. Open the newly created controller file and extend Laravel's default Auth controller:
1
2
3
4
5
6
7
8
9
namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppHttpControllersController;

class CustomAuthController extends Controller
{
    // Your custom login logic will go here
}


  1. Then, inside your CustomAuthController, you can override the login method with your custom logic:
1
2
3
4
public function login(Request $request)
{
    // Your custom login logic here
}


  1. Lastly, update Laravel's route to use your custom controller instead of the default Auth controller. You can do this by updating the routes/web.php file:
1
2
3
Auth::routes();

Route::post('login', 'CustomAuthController@login')->name('login');


Now, when a user tries to log in, your custom login logic will be executed instead of Laravel's default login function. You can add your own custom validation, authentication, or any other logic that you need for the login process.