3 Frequently Used Laravel Routing Methods

Show data with the default properties you want, not only that. Scope can also help us in making our code more readable.Routing is a fundamental feature in creating applications using the Laravel framework, so here we will discuss some features that you can do when you want to create routes.

Route Resource & Api Resource

This route is usually used to create CRUD (Create, Read, Update, Destroy). So instead of defining routes one by one, it's better to group them into resources.

When we use a resource, it will automatically generate 7 urls and methods on the controller that you are aiming for. In this example, we will create a controller for Post. ``php Route::resource('posts', PostController::class);

Then, if you list the route specifically on the `posts`, then you should see this.
artisan route:list --name=posts
GET|HEAD    posts ...  posts.index › PostController@index
POST        posts ... posts.store › PostController@store
GET|HEAD    posts/create ... posts.create › PostController@create
GET|HEAD    posts/{post} ... posts.show › PostController@show
PUT|PATCH   posts/{post} ... posts.update › PostController@update
DELETE      posts/{post} ... posts.destroy › PostController@destroy
GET|HEAD    posts/{post}/edit ... posts.edit › PostController@edit

Now, then, if you want to create the controller, it's easy by flagging -model=[] like artisan make:controller PostController --model=Post.

Okay, now let's talk about Api Resource, actually it's not that different, if we create Route::apiResource() it will generate 5 methods only, without create and edit.

Route::apiResource('posts', PostController::class);

Then, if we want the controller generation, don't forget to add the api flag as well like artisan make:controller PostController --model=Post --api.

Route Group

For this technique, you might have used it many times, because it is also commonly used to group routes for example in middleware, namespace, controller, name, namespace, etc.

Route::middleware('auth')->group(function () {
    Route::get('dashboard', DashboardController::class);
    Route::get('account/edit', [ProfileInformationController::class, 'edit']);
    Route::put('account/edit', [ProfileInformationController::class, 'update']);

Route::middleware('guest')->group(function () {
    Route::get('login', LoginController::class);
    Route::get('register', RegisterController::class);

Route Controller

If you have created repeated routes with the same controller, then you can now group them by grouping them by controller.

# Before
Route::get('account/edit', [AccountController::class, 'edit']);
Route::put('account/edit', [AccountController::class, 'update']);
Route::get('password/edit', [AccountController::class, 'editPassword']);
Route::put('password', [AccountController::class, 'updatePassword']);

# After
Route::controller(AccountController::class)->group(function () {
    Route::get('account/edit', 'edit');
    Route::put('account/edit', 'update');
    Route::get('password/edit', 'editPassword');
    Route::put('password', 'updatePassword');

Well, that's all for this article, hopefully this can be useful.

Find some typo, just make the pull request on Github.
© 2022 Irsyad Notes. All rights reserved.