Laravel auth scaffolding tutorial (Laravel Login and registration example) – Laravel is a free, open-source PHP web framework, intended for the development of web applications following the MVC architectural pattern.
Prior to Laravel 6, we had a different way to create an auth scaffolding. However, in later versions, it has been moved to a separate package: laravel/ui. After the update, it has added more flexibility and some additional steps as well. But, you don’t need to worry as we have added the detailed guide so that you can create a scaffolding without any hassle.
For the detailed instructions on how to install Laravel on windows, you can refer to our previous article. On this tutorial, we will solely focus more on creating Laravel auth scaffolding, so we are not going to describe the installation process here in detail.
To create a laravel auth scaffolding, we have simplified the procedure in the following steps:
Table of Contents
#1. Create new laravel app
You can create a brand new Laravel application with the command:
laravel new laravel-auth-scaffolding
In case if you have not installed the global laravel installer yet, you can create a new laravel project using the composer as well.
composer create-project --prefer-dist laravel/laravel laravel-auth-scaffolding
#2. Change directory to the recently created app folder
Once the laravel auth scaffolding app is created, we need to change the directory to the recently created “laravel-auth-scaffolding” folder.
cd laravel-auth-scaffolding
#3. Install laravel/ui package via composer
After changing the directory, the first thing we need to do is installing the laravel/ui package, and this could be installed by using composer as follows:
composer require laravel/ui
This Laravel’s laravel/ui package provides a quick way to scaffold all necessary views as well as necessary routes for the authentication flow.
#4. Generate laravel auth scaffolding including your preferred frontend scaffolding
After installing the package, we can install one of the frontend scaffolding directly using commands like “php artisan ui bootstrap” or “php artisan ui vue” or “php artisan ui react”.Â
However, instead of installing just the basic scaffolding, we can also install the laravel auth scaffolding (login/registration) directly as well. And, to install this we just need to add “–auth” at the end of each scaffolding option.
// Generate login / registration scaffolding... php artisan ui bootstrap --auth php artisan ui vue --auth php artisan ui react --auth
In this tutorial, we will select bootstrap as frontend scaffolding, so will describe in detail only about this process.
By the way, this command should be used on freshly installed applications. This command will create a layout view, registration, and login views, as well as routes for all authentication end-points ie. “Auth::routes()”. To handle the post-login requests ( any requests after successful login ) one HomeController will be generated as well. On top of that, this package will also generate several pre-built authentication controllers, that can be found in the “App\Http\Controllers\Auth namespace”.
There are basically four controllers:
RegisterController: handles registration for a new user
LoginController: handles authentication for the app
ForgotPasswordController: handles password resetting mechanisms, which include sending resetting email links
ResetPasswordCondroller: contains the main logic of resetting passwords
In case if your application does not offer the registration facility, you can disable this feature by modifying your route declaration “Auth::routes([‘register’ => false]);” on “routes/web.php”. In addition to that, you can also delete the RegisterController as well.
#5. Compile SASS to plain CSS
Next, we need to compile the SASS. But, before compiling your SASS, install the project’s frontend dependencies (bootstrap in our case) using the Node package manager (NPM):
npm install
Once the dependencies have been installed using, you can compile your SASS files to plain CSS using Laravel Mix as:
npm run dev
This command will process the instructions in your webpack.mix.js file. Typically, this compiled CSS will be placed in the “public/css” directory.
On alternative to the above-mentioned methods, if you want to create the application including laravel authentication scaffolding, you may use the “–auth” directive while creating the laravel application.
laravel new laravel-auth-scaffolding --auth
This command will create a brand new laravel application with all of the authentication scaffolding compiled and installed.
#6. Update .env file and run migration
Next, we need to update the “.env” files with correct database credentials.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_as_db //database name for your project DB_USERNAME=root DB_PASSWORD=
After updating the credentials, let’s run the migration command so that the necessary tables may be created on the database.
php artisan migrate
#Note: In case if your server configuration possesses MySQL older than the 5.7.7 or MariaDB older than the 10.2.2 release, you need to manually configure the default string length. You can set this default string length in “app/Providers/AppServiceProvider”
use Illuminate\Support\Facades\Schema; /** * Bootstrap any application services. * * @return void */ public function boot() { Schema::defaultStringLength(191); }
Now, it’s all done. You can start your local development server with:
php artisan serve
This will start a local development server on “http://localhost:8000” i.e “http://127.0.0.1:8000”.
Now, you can test all by yourself by registering a new user and login with the registered credentials. You can register to the web through the link http://localhost:8000/register as well as from the register link provided by the app itself.
After, successful register you will be redirected to the dashboard page that shows “You are logged in!“.
Similarly, you can log in via http://localhost:8000/login. After successful login, you will be redirected to the same dashboard page just like the registration.
This successful registration and login mean you have successfully created the laravel auth scaffolding app. Furthermore, you can explore it yourself and create the app that fulfills your requirements.