#12 Laravel 6 Basics | Resource Controllers (II) | Partial/Nested/Naming Resource Routes

preview_player
Показать описание
In Part-12 of Laravel 6 Basics, we will keep learning Controllers in detail.

We will keep experimenting with lot of functionalities in new Laravel 6 website and in E-com series.

In this video, we will continue working on Resource Controllers.

Partial Resource Routes
Sometimes while making a resource controller, you require not all but few functions so Laravel makes this possible with Partial Resource Routes that can be created with only and except options.

Sometimes while making a resource controller, you don't want create and edit functions so Laravel makes this possible with apiResource method that automatically exclude these two routes like below :-
Route::apiResource('photos', 'PhotoController');

You can create many API resource controllers at once by passing an array to the apiResources method.

To quickly generate an API resource controller that does not include the create or edit methods, use the --api switch when executing the make:controller command like below:
php artisan make:controller API/PhotoController --api

Nested Resources
Sometimes we require to create routes within another resource. Like photo resource with multiple "comments" can be declared like below with "dot" notation in route :-

This route will register a "nested" resource that can be accessed with URLs like below:

photos/{photos}/comments/{comments}

When you run above route, show function will execute and both photo and comment will return to function as parameters.

Naming Resource Routes
By default, all resource controller actions or functions have route name but we can change as per our own requirement.

create function will display below photos create url :-

Naming Resource Route Parameters
By default, Route::resource will create the route parameters for your resource routes based on the "singularized" version of the resource name.

Like if photos is the resource controller then it will pick photo as parameter as photo is the singular of photos.

Please run below artisan command
php artisan route:list

We can see in URI, it will show the parameter as {photo} by default as resource route is photos.

We can easily override this by using the parameters method. Like we have override photos to admin_photos as parameter in resource controller.

After running "php artisan route:list" command again, we can see in URI, it will show the parameter as {admin_photos} instead of {photo}.

Localizing Resource URIs

By default in Resource Controllers/Routes, resource URIs are in English like create and edit. We can update to our own local language by adding resourceVerbs method in boot method of AppServiceProvider.

Like we can replace "create" and "edit" URIs with Spanish words like "crear" and "editar" respectively.

Don't forget to add "use Route;" namespace at top of AppServiceProvider file.

Once the URIs have been customized, a resource route such as Route::resource('photos', 'PhotoController') will produce the following URIs:

/photos/crear in place of /photos/create

/photos/{photo}/editar of /photos/{photo}/edit

Supplementing Resource Controllers
In case of additional routes in resource controller, we should define those routes before default Route::resource; otherwise, additional routes might now work and replaced by default resource routes.

Like in case of add-photo, update-photo and delete-photo routes, we will add them above resource routes.
Рекомендации по теме
Комментарии
Автор

Nested Resources start in minute 14:09

mohamed-pjqw