Customizing Laravel's Auth Scaffold
Here's how to customize Laravel's Auth scaffold if you want to:
- use firstname and lastname (and/or middlename) instead of Laravel's default name field
- group the Laravel auth routes behind a user/ prefix (e.g. user/login, user/dashboard, etc.)
Use firstname and lastname fields
database/migrations/2014_10_12_000000_create_users_table.php
Change name in Schema::create to firstname and lastname:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name'); // <-- Keep this in for Laratrust
$table->string('name_first');
$table->string('name_middle')->nullable(); // <-- optional
$table->string('name_last');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
app/User.php
Modify User nodel by changing:
protected $fillable = [
'name', 'email', 'password',
];
to:
protected $fillable = [
'name_first', 'name_middle', 'name_last', 'email', 'password',
];
App/Http/Controllers/Auth/RegisterController.php
Change validator() and create() methods:
protected function validator(array $data)
{
return Validator::make($data, [
'name_first' => ['required', 'string', 'max:255'],
'name_middle' => ['string', 'max:255'],
'name_last' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
protected function create(array $data)
{
return User::create([
'name_first' => $data['name_first'],
'name_middle' => $data['name_middle'],
'name_last' => $data['name_last'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
views/auth/register.blade.php
Change form input markup to include name_first, name_middle and name_last.
Group all auth routes behind a user/ prefix
Create a separate User/Controllers directory
Create DashboardController in User/Controllers directory
Make sure it's properly namespaced
namespace App\Http\Controllers\User
Make sure to include the Controller from the correct namespaced path:
use App\Http\Controllers\Controller;
Make sure the index() method returns a view of user.dashboard
return view('user.dashboard');
Put Any Other User Controllers in separate /User directory
Make sure to namespace the controllers properly: namespace App\Http\Controllers\User
App/Http/Controllers/Auth/LoginController.php
Change:
protected $redirectTo = '/home';
to:
protected $redirectTo = '/user';
Add the following inside the class:
protected $redirectPath = 'user';
protected $loginPath = 'user/login';
protected $redirectAfterLogout = 'user/login';
App/Http/Controllers/Auth/ResetPasswordController.php
Change:
return redirectTo = '/user';
App/Http/Middleware/RedirectIfAuthenticated.php
Change:
if (Auth::guard($guard)->guest()) {
if (Auth::guard($guard)->check()) {
return redirect('/user');
}
}
routes/web.php
Although there's probably a better way to do this, change the route for home:
Route::get('home', function() {
return redirect('/user');
});
Place all your user routes inside a "user" route group:
Route::group(array('prefix' => 'user'), function()
{
// Place all your user routes here
});
Add named get & post routes inside this route group for user/logout:
Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout')->name('logout');
Route::post('logout', '\App\Http\Controllers\Auth\LoginController@logout')->name('logout');
Also add a route group inside this route group for any routes that need to be protected by auth middleware:
Route::middleware(['auth'])->group(function() {
Route::get('/', 'User\DashboardController@index');
});
And add a route outside the route group for just simply logout:
Route::get('logout', function() {
return redirect('user/logout');
});
Create /user directory in /views
Move app.blade.php from /layouts to /user/layouts
resources/views/user/layouts/app.blade.php
Add user to URLs as needed:
url('user')
url('user/login')
Create a dashboard.blade.php view inside the /user directory
resources/views/auth/passwords/email.blade.php
Change the extends file to the correct location:
extends('user.layouts.app')
resources/views/auth/passwords/reset.blade.php
Change the extends file to the correct location:
extends('user.layouts.app')
resources/views/auth/login.blade.php
Change the extends file to the correct location:
extends('user.layouts.app')
Add user/ to all the other URLs as needed:
url('user/password/reset')
resources/views/auth/register.blade.php
Change the extends file to the correct location:
extends('user.layouts.app')