Waldemar's avatar

Laravel 5.3 - Routing as Auth::routes();

Hi! When i used php artisan make:auth in my routes/web.php added line Auth::routes(); But i cant find the list routes for Auth::routes(); can i use this method in my controllers and how?

I this it`s so cool if i ussed Products::routes(); Auth::routes();

0 likes
10 replies
hxd's avatar

I made a new system for admins, copy from /app/Controller/Auth/, like this :

Route::get('admin/register', 'Admin\RegisterController@showRegistrationForm');

Route::post('admin/register', 'Admin\RegisterController@register');

Route::get('admin/login', 'Admin\LoginController@showLoginForm');

Route::post('admin/login', 'Admin\LoginController@login');

Route::post('admin/logout', 'Admin\LoginController@logout');

and you can read the code and find these functions it used. like LoginController.php

use AuthenticatesUsers;

find this file and read it codes, you will see the functions used and what this function do, forgive my poor English.

1 like
Waldemar's avatar

Ok i find this code

public function auth() { // Authentication Routes... $this->get('login', 'Auth\LoginController@showLoginForm')->name('login'); $this->post('login', 'Auth\LoginController@login'); $this->post('logout', 'Auth\LoginController@logout'); // Registration Routes... $this->get('register', 'Auth\RegisterController@showRegistrationForm'); $this->post('register', 'Auth\RegisterController@register'); // Password Reset Routes... $this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm'); $this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail'); $this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm'); $this->post('password/reset', 'Auth\ResetPasswordController@reset'); }

but this code in facade framework ' framework/src/Illuminate/Routing/Router.php '

can i write this code in other files?

1 like
dmeganoski@gmail.com's avatar

I might be able to help, but I'm not sure exactly what it is you're trying to do.

Do you want to simplify your web.php routes file?

Or are you trying to find the functions that these routes are calling? (I.E. @showLinkRequestForm)

Waldemar's avatar

sorry for my english

I want to bring all the separate routed to different files as example Auth::routes();

i need use Products::routes() how to do it?

ANONYMOUSGDPRUSER4552's avatar

The direct answer to you question is: The location of all Auth:routes(); routes is:

/vendor/laravel/framework/src/Illuminate/Routing/Router.php

then search for the auth() method in the Router.php file and you'll see all the routes created by the Auth.

Hope this helps :)

4 likes
on3nx's avatar

i just wondering, what is the purpose of this Auth::routes() ?

will it the same if i put:

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

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

    // Password Reset Routes...
    Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm');
    Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail');
    Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm');
    Route::post('password/reset', 'Auth\ResetPasswordController@reset');

in web.php?

while i saw: Route::get('/home', 'HomeController@index'); in web.php

so, should i do my route in auth.php or in web.php?

TiborBesze's avatar

@on3nx Auth::routes() is just a shorthand for the routes you've mentioned. You could either put those lines in your routes/web.php, or leave Auth::routes() in place. It's just easier for Laravel to scaffold the Authentication routes by only adding 2 lines to your routes/web.php.

2 likes
dmeganoski@gmail.com's avatar
Level 4

@Deamonik

I get what you mean, now. You're looking for an easy way to define all the routes for your other objects, I.E. Products, so you could use Product::routes().

Well, sure, you could make your own routes() function that does the same thing. The question is, where to put it, then. Auth refers to a facade, not an actual class, which is why you can access the non-static function statically.

If you really wanted to, you could create static methods on each of your controllers and call that in your web.php.

I.E.

static function routes() {
        Route::group(array('prefix' => 'product'), function() {
            Route::get('/{id?}', array('as' => 'product.index', 'uses' => 'ProductController@index'));
        });
        
    }

and then in web.php

\App\Http\Controllers\ProductController::routes();

@on3nx @tiborbesze86 is correct, the function exists to make the transition less painful and keep your routes file tidier. You can copy over all the routes from the function into the web.php file directly so that you can modify them, if you like. Then, just remove the Auth::routes() call.

You never want to modify any of the files inside the 'vendor' directory, unless you expect it to be a temporary thing. Those can be easily overwritten with an update to a composer package.

6 likes
redbonzai's avatar

In case you were curious, you could find the list of routes that are exported to the web.php route file here : Illuminate/Routing/Router.php

search for auth() on line approx: 1129.

public function auth()
{
    // 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');
}

Please or to participate in this conversation.