Writing Routes in Laravel 6 and Laravel 7. Start by Learning This Basic Format.
Thank you for your continued support.
This article contains advertisements that help fund our operations.
Table Of Contents
This article summarizes the simplest way to write routes in Laravel 6 and Laravel 7.
For information on routing changes from Laravel 8 onward, please refer to the following article:
Basic Routing in Laravel 6
Routing Accessible to Everyone
Route::get('/home', 'HomeController@index')->name('home');
Routing Requiring Authentication
Route::middleware(['auth'])->group(function () {
Route::post('/comment', 'CommentController@store')->name('comment.store');
});
⇨ Routing for Authenticated Users Only
Key Points
Specifying the URL
/home
This specifies that the process should be returned to users accessing the /home
URL.
Specifying the Controller
'HomeController@index'
This returns the processing of the index
function within HomeController
to users who access the URL.
Using name
->name('home')
By including this notation, you can use a helper function to output the URL, for example:
route('home') // Output: 'http://localhost/home'
Naming routes increases flexibility and can reduce work in case of future changes.
Routing with name
for Beginners
What is Routing?
Routing is responsible for assigning what process to return when a user accesses a URL.
From the perspective of someone creating an app, it feels like "creating URLs."
In Laravel, routing is described in files under the routes/
directory, such as:
routes/web.php
⇨ Laravel 6.x Routing Documentation
Routing Style in Laravel 8 and Later
use App\Http\Controllers\PostController;
Route::get('/posts', [PostController::class, 'index'])->name('post.index'); // Add a name
Conclusion
Although fewer people may still be using Laravel 6, there seems to be some demand, so I’ve left this article available.
For those using Laravel 8 or later, please be aware of the differences.