Welcome

Welcome to George's Amazing World blog. Here you will enjoy what life has to offer and relax your mind after a sweatily day of work

September 7, 2017

REST Killing Laravel



After hearing about the Laravel hype, sorry, Laravel power, I decided to join the bandwagon. Of course Site Point ranks it number and other sites will rate other frameworks highly. Get me well on this, it is not Site Point alone but many other sites and among these Value Coders does a good job in explaining why Laravel is the best framework. And oh! Coder’s Eye comes with a good graphical representation of the fact – for those who love statistics.
So I started my reading journey, I grabbed a copy Laravel 5.2 and Laravel 5.3 but running examples from 5.3 code base and off I started. It was all interesting. After understanding controllers, I made a choice – I will use Route::controller() because it looks focused so it would help me manage application resources better, but Boo!, the nice feature is deprecated 5.3 – so frustrating!
To understand my frustration well, let’s have a small recap of how Laravel MVC works. In Laravel, everything is a user accesses (via the browser client) is resource stored on the server. The client can request for the resource through a URI. The client must specify operation (Create, Update etc) it wants to carry out on the resource. Whether the client can access the resource and carry out the intended operation is controlled by the Controller and Middleware.    

The URIs are implemented as routes. So for every URI you predict your web application to have, there has to be some kind of route defined for it. So for 40 URIs, you need 40 routes defined – this is tedious, not so? This was,however, solved by introducing resource and controller routes. You could still decide to define all the 40 routes or take advanced of the these two advanced features (Route::controller() or Route::resource()) . But now you can only take advantage of Route::resource().

Controller routes would allow you to implicitly access URIs attached to the controller by defining only one route and the only requirement would be to have a method in the controller starting with one of the get or post (If you started the method name with get, it will handle only get requests and if it started with post then it will handle the post request). For example, for a route like Route::controller('test', 'CompanyController'), you could access the company resource like http://example.com/test/profile  as long as you had a method like getProfile() or postProfile() defined in Companycontroller. http://example.com/test/delete, would mean there is a method named getDelete() or postDelete() in CompanyController. This was really nice, not only for implementation but also for organization. You could write your controller in such a way that all actions/operations for a resource are in one controller and accessible through one Route!!

Do you know why I writing this in past tense? Because it was deprecated in version 5.3 so if you still need it just don’t upgrade from Laravel 5.2 but you are not alone.  And do you know why it was deprecated? Well I have not yet found out. I am still asking the fountain of knowledge, Google but from what I am seeing, it is because it was violating REST. So all we are left with are resource routes.

In its effort for to implement REST, Laravel provides for resource routes. Like Controller routes, you can access resource routes through a single Route e.g., Route::resource(‘profile’, ‘ProfileController’). Unlike controller routes (Route::controller()), resource routes (Route::resource) Laravel defines specific methods e.g., in our ProfileController you can only have the following methods: index(), create(),store(Request $request), show($id), edit($id), update(Request $request, $id), destroy($id)! So if you visit http://example.com/profile/destroy/3, the controller has enough intelligence to call destroy() method of the controller.  And if you want to have any additional method in the controller apart from those provided by Laravel, you must first define a route for it. E.g., if we wanted to have a URL like http://example.com/profile/approve, then we would have to define a method such as approval()  in our ProfileController, then we would have the following routes.
Route::get(‘profile/approvals’, ‘ProfileControler@approve’)
Route::resource(‘profile, ‘ProfileController’)

Laravel versions greater than 5.2 are moving towards RESTful CRUD applications i.e., an application’s resources can be Created, Read, Updated or Destroyed (CRUD) and these can be accessed using the defined HTTP verbs such as POST, GET, PUT etc.

Conclusion

Much as the major operations on a resource are CRUD, even the “minor operations” are necessary for the creation of a meaningful and usable application. So either ask HTTP, which informed REST, to add more verbs. Asking me to explicitly add a route like Route::get(‘profile/approvals’, ‘ProfileControler@approve’)  to my routes (because Route::resource() does not support it) makes me feel like am hacking my own system. And defining a different controller for the same resource so that I can make use of the provided CRUD methods makes me feel like I am handling a different application resource.

Let us not rush things, even the mother language of the web, HTML is still using only POST and POST methods in its forms. Or are we rushing to be the first one to implement the HTTP spec?
So please bring back our Route::Controller()!!

No comments:

Post a Comment