Learner
Learner

Reputation: 773

Laravel routes the right or better way

I am new with Laravel and I was wondering is there a big difference in using routes basically and with my way:

Instead of using:

Route::get('/','PagesController@index')->name('home');
Route::get('/about','PagesController@about')->name('about');
Route::get('/contact','PagesController@contact')->name('contact');

And methods just return views...

I am using:

Route::get('/{page?}','PagesController@index')
->where(['page'=>'about|contact'])->name('page');

public function index($page = 'home') {
    return view($page);
}

With second example I can remove a lot of lines of code, since resource controller is not the best option for my needs.

So are there any bad reasons why shouldn't I use routes my way?

Thanks

Upvotes: 1

Views: 297

Answers (2)

Rwd
Rwd

Reputation: 35180

Since it doesn't take that much time or effort, I would also suggest being explicit as this gives you much more control in the future. Plus with your current implementation you're going to have to add to where clause every time you want to add a new page so it wouldn't be saving you that much time anyway.

One way to simplify your routes file would be to use view() rather than get():

Route::view('about', 'about')->name('about');

An middle of the road option would be to add your simple views to an array and have something like:

$basicViews = ['about', 'contact'];

foreach ($basicViews as $basicView) {
    Route::view($basicView, $basicView)->name($basicView);
}

I'm not saying you should go with this approach but there are benefits in comparison to using a PageController and a regex string:

  • All of the routes involved can clearly be seen and everything is contained in your routes file.
  • Routes can still be named.
  • You don't have to worry as much about potentially have a very long regex string

If you need to change the uri, blade file name or route name then I would definitely suggest going be to being explicit rather than trying to overcomplicate things.

Upvotes: 1

matpb
matpb

Reputation: 476

I don't see any evident security issues or logical faults to declaring routes your way.

TL;DR

There are reasons Laravel does its things a certain way; following standards might prove useful down the line.

On the other hand, I can some a few advantages to writing those few lines of code you are trying to avoid:

1. Readability

It's quite easy to see a list of available pages when they are lined up like in the first example, versus digging through the where call and its argument.

2. Maintenance and upgradability

For you, at this point in time, it seems like the easiest and fastest way to use a dynamic page caller, but for you in 2 years, or another programmer, it might make a lot more sense to have all the routes broken down into smaller pieces. What you aren't implementing yet doesn't mean it will never be. If you need to add some variables to your views later on, it will be much easier to jump into that method call and add them.

3. Using named routes

In an eventual case where you'd need to change some Urls, using named routes in your blade templates would save you time; a technique you can't really use with a dynamic page caller.

Upvotes: 1

Related Questions