Thursday 16 March 2017

Topic 10 - Explicit , Implicit & Restful Resource Controller

EXPLICIT CONTROLLER

In every laravel project explicit controller is very common routing that developer team use.
Explicit routing is pointing every action ( GET, POST) to a function of a controller. Explicit controller provide a clearer view function and routing name. Explicit controller was use to add additional method or method with specific naming convention.

Example of explicit controller that can be find in my project.

//return customer list
Route::get('customers', 'CustomerController@index');
//create customer form
Route::get('/create','CustomerController@showCustomerForm');

//store customer form
Route::post('/create','CustomerController@store');
IMPLICIT CONTROLLER (Deprecated)

Implicit controller is flexible because it allows you to easily define a single route to handle every action in a controller. You can define the route using the Route::controller method. The controller method accepts two arguments, first is URI the controller handles, while the second is the class name of the controller. 

According to laravel 5.0 documentation : 
If your controller action contains multiple words, you may access the action using "dash" syntax in the URI. For example, the following controller action on our UserController would respond to the users/admin-profile URI:

public function getAdminProfile() {}

However implicit controller deprecated in Laravel 5.2. The call Route::controller was just added as an extra helper.

RESTFUL CONTROLLER

A restful controller can be found with some default routes and naming action .

Using syntax Route::resource('customers', 'CustomerController'); eventually will call default @index action which located in CustomerController. 

Actions Handled By Resource Controller

VerbURIActionRoute Name
GET/customerindexcustomer.index
GET/customer/createcreatecustomer.create
POST/customerstorecustomer.store
GET/customer/{customer}showcustomer.show
GET/customer/{customer}/editeditcustomer.edit
PUT/PATCH/customer/{customer}updatecustomer.update
DELETE/customer/{customer}destroycustomer.destroy
  
According to laravel  5.4 documentation HTML forms can't make PUTPATCH, or DELETE requests,  from here we neeed to add spoofing method in <form> tag. The method field can be define like below  :
{{ method_field('PUT') }}

No comments:

Post a Comment