Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the login/register URL in Laravel 5.2

Tags:

php

laravel

I'm currently writing an application which only has accounts for staff of the company, not regular website visitors. As such, I would like to keep my URLs which relate to the 'admin' area of the site, under the /admin URL which means changing /login to /admin/login and /register to /admin/register.

However, I'm at a loss as how to change the login and register URLs in Laravel 5.2, it seems that in previous versions it was a simple matter of adding protected $loginPath = '/admin/login'; to Auth\AuthController However, this makes no difference when adding it to my AuthController.

The following is the output of php artisan route:list;

+--------+----------+-------------------------+------+-----------------------------------------------------------
| Domain | Method   | URI                     | Name | Action
+--------+----------+-------------------------+------+-----------------------------------------------------------
|        | GET|HEAD | /                       |      | App\Http\Controllers\PageController@index
|        | GET|HEAD | admin                   |      | App\Http\Controllers\AdminPageController@index
|        | GET|HEAD | admin/profile           |      | App\Http\Controllers\AdminPageController@profile
|        | GET|HEAD | login                   |      | App\Http\Controllers\Auth\AuthController@showLoginForm
|        | POST     | login                   |      | App\Http\Controllers\Auth\AuthController@login
|        | GET|HEAD | logout                  |      | App\Http\Controllers\Auth\AuthController@logout
|        | POST     | password/email          |      | App\Http\Controllers\Auth\PasswordController@sendResetLink
|        | POST     | password/reset          |      | App\Http\Controllers\Auth\PasswordController@reset
|        | GET|HEAD | password/reset/{token?} |      | App\Http\Controllers\Auth\PasswordController@showResetForm
|        | GET|HEAD | register                |      | App\Http\Controllers\Auth\AuthController@showRegistrationF
|        | POST     | register                |      | App\Http\Controllers\Auth\AuthController@register
+--------+----------+-------------------------+------+-----------------------------------------------------------
like image 743
dwilson390 Avatar asked Feb 09 '16 17:02

dwilson390


People also ask

How do I change my auth login URL in Laravel?

Override with: Route::get('/admin/login', 'Auth\AuthController@showLoginForm'); Route::post('/admin/login', 'Auth\AuthController@login'); Route::get('/admin/logout', 'Auth\AuthController@logout'); Note: Route::auth() automatically handles login, logout and register.

What is auth () in Laravel?

Laravel includes built-in authentication and session services which are typically accessed via the Auth and Session facades. These features provide cookie-based authentication for requests that are initiated from web browsers. They provide methods that allow you to verify a user's credentials and authenticate the user.


2 Answers

In L5.2, all authentication routes are grouped in a route called Route::auth().

If you search for the symbol auth() , you can see the auth() function which contains all the route links.

Check this file: Illuminate\Routing\Router for the auth() function.

But I'm not sure if it's a good practice to change it here.

Edit:

So if you need to change the default auth routes, include all routes in your routes.php file and change the uri as you want it.

To get a reference:

    // Authentication Routes...
    Route::get('login', 'Auth\AuthController@showLoginForm');
    Route::post('login', 'Auth\AuthController@login');
    Route::get('logout', 'Auth\AuthController@logout');

    // Registration Routes...
    Route::get('register', 'Auth\AuthController@showRegistrationForm');
    Route::post('register', 'Auth\AuthController@register');

    // Password Reset Routes...
    Route::get('password/reset/{token?}', 'Auth\PasswordController@showResetForm');
    Route::post('password/email', 'Auth\PasswordController@sendResetLinkEmail');
    Route::post('password/reset', 'Auth\PasswordController@reset');
like image 119
Jilson Thomas Avatar answered Oct 09 '22 15:10

Jilson Thomas


You can call Route::auth() inside a prefixed group.

Route::group(['prefix' => 'admin'], function() {
    Route::auth();
});
like image 39
indyo Avatar answered Oct 09 '22 16:10

indyo