Leandro_Haruki's avatar

Is there any route alignment/indentation pattern? (and more questions)

Hello guys, i would like to know some nice way to set up my routes, since i have started learning Laravel my routes always felt really messy and unaligned.

This is how my routes usually look:

Route::post('password/reset', 'Auth\ResetPasswordController@reset')->name('password.reset');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
Route::post('signup', 'Auth\AuthController@register')->name('signup');

Since i'm still new to the framework, i've been changing my routes time to time due to namespaces, Model bindings instead of putting it in the Request body, etc. So i thought about aligning routes like:

Route::post('password/reset',   'Auth\ResetPasswordController@reset')                 ->name('password.reset');
Route::post('password/email',   'Auth\ForgotPasswordController@sendResetLinkEmail')   ->name('password.email');
Route::post('signup',           'Auth\AuthController@register')                       ->name('signup');

I think that aligning the routes parameters would make the routes look better, however, i'm afraid it will be a pain to keep everything aligned.

I would like to know how you guys manage routes:

  • Should i separate my api routes in different route files?
  • Is this alignment thing bad?
  • Should i follow a single pattern of using only Route::resource, separate every request type or mix it when needed?
  • I usually set a middleware to groups only and put it in controllers constructors to single routes, is it bad for code maintenance? (is there a better way?)

TL;DR: I'm a Laravel noob and my api routes file look ugly and long.

EDIT: Added questions & title change.

0 likes
4 replies
jlrdw's avatar

I never put very much thought to it as they are all on the same level.

Cronix's avatar
Cronix
Best Answer
Level 67

As with the rest of your code, it's just personal preference. I do tend to do what you did in your 2nd example, where I put the controller indented so they are all on the same "level", but I don't with the ->name() attribute. But no, there is no specific "convention" or "best practice". PHP doesn't care.

braed's avatar

I really like your second example but I can't do it without needing to wrap or scroll as I usually have my project browser open, reducing screenspace.

Leandro_Haruki's avatar

Thank you for the replies, i'll do as @Cronix said and just indent the controllers, it sounds cleaner.

Please or to participate in this conversation.