How to edit the routes created by make:auth?

Asked

Viewed 3,927 times

3

I have a project at Laravel where I used the command make:auth to create the views, routes, controllers etc, a registration form.

I need to change the a view which is used by default in login for a view that created the hand.

Doubt:

  • You can edit this view or route?
  • If yes, where?

2 answers

5

This will depend on the version of Laravel you are using.

Laravel 5.2

You need to add property $loginView in class AuthController.

Thus:

protected $loginView = 'nome_da_minha_view';

In the source code of trait called Illuminate\Foundation\Auth\AuthenticatesUsers, there is a method called showLoginForm, that’s written like this:

/**
 * Show the application login form.
 *
 * @return \Illuminate\Http\Response
 */
public function showLoginForm()
{
    $view = property_exists($this, 'loginView')
                ? $this->loginView : 'auth.authenticate';

    if (view()->exists($view)) {
        return view($view);
    }

    return view('auth.login');
}

note that if the property exists loginView in the current class (in this context it would be AuthController), the view used will be the one defined in this property, if the view also exists (according to the view()->exists().

Laravel 5.3 and 5.4

You need to simply overwrite the called method showLoginForm in the authentication class and return to view desired.

  public function showLoginForm()
  {
        return view('minha_view_personalizada');
  }

There is another tip, which is to create the method called getLogin returning to view. It will have the same effect as shown above with showLoginForm.

  • My Laravel is in version 5.4... So where do I find this 'showLoginForm''?

  • My project n has Authcontroller

3


In this specific case to change the views you will want to follow the recommendation of friend @Wallace Maxters. This is the recommended way. ;)

If you need to customize routes, you can simply comment on the line Auth::routes() of the archive routes/web.php and declare the routes manually by following the model of how they were declared (I’m using Laravel 5.4 as a reference):

// Authentication Routes...
$this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
$this->post('login', 'Auth\LoginController@login');
$this->post('logout', 'Auth\LoginController@logout')->name('logout');

// Registration Routes...
$this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
$this->post('register', 'Auth\RegisterController@register');

// Password Reset Routes...
$this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
$this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
$this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
$this->post('password/reset', 'Auth\ResetPasswordController@reset');

To not need to change anything in the code you just need to keep the names of the routes and keep the same URL on the routes that repeat the URL, so that you can for example translate the URL login for entrar provided you do this both on the GET route and on the POST route.

In general you don’t need to change the methods that the routes point to since you can overload them directly in the classes that they depend on. ;)

  • 1

    It is always interesting to bring information about how the framework works internally. + 1

  • 1

    Particularly, I always delete everything from Laravel authentication and do by hand. I always need to customize something, kkkkkkk

  • How do p/ after me log in or register some user, return to a view created by me and not the home?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.