Laravel: Route

1 minute read

What is Routing?

Routing is simply forwarding the requests made or received to their respective actions or functions.

A simply route function

Route::get('/', function () {
    return "Hi";
});

The example above is a route using get method, and get method requires the route itself (parameter), then the action. The basic action is usually a closure function, and we returned a string.

Route Grouping

This one facinated me, as it was not covered on my first course in Laravel, so its a new perk for me.

What is Route Grouping?

Route grouping is just wrapping related routes parameter into a single group. Example speaks better than words..

Take for instance, instead of doing this:

Route::get('/customer', function() {
    return 'Home';
});
Route::get('/customer/create', function() {
    return 'create';
});

We can do this instead.

Route::group(['prefix' => 'customer'], function() {
    Route::get('/', function() {
        return 'Home';
    });
    Route::get('create', function() {
        return 'create';
    });
});

Route grouping is not faster per se, but it just organizes your code in a readable format.

Route Fallback

Have you ever visited a broken link and you saw an error 404 page not found? Thats the concept Route Fallback is talking about. It just throws something when a broken link or a non-existing page request was made.

Simply Route fallback:

Route::fallback(function() {
    return "Route does not exist";
});

Conclusion

That would be all for the beginning of Routing, I look forward in seeing what Laravel has in stock.

See you next time I get the opportunity to continue my course.

Leave a comment