Gabriel Matusevich
Gabriel Matusevich

Reputation: 3855

Laravel Route Params and Form Params

I have a Backend in Laravel, which is basically a REST API, because I'm using AngularJS in the FronEnd and making ajax requests.

Let's say I have to make a simply CRUD for Users

And I don't know if there is any difference between putting some of the parameters in the Route itself or all of them in the Form Input.

For Example:

Route::post('/Users/Update', 'UsersController@update);

And then call the 'id' parameter from:

Input::get('id')

or

Route::post('/Users/Update/:id', 'UsersController@update);

and include it as a parameter of the function update like:

public function update($id) { }

Is there any real difference between this two ways? maybe security issues? coding standards? or is it the same?

Should I just use Laravel's REST controllers?

Upvotes: 3

Views: 311

Answers (2)

Kirill Fuchs
Kirill Fuchs

Reputation: 13686

The concise some-what opinionated answer:

You should define your route as:

Route::put('/users/:userId', 'UsersController@putUser');

Your public function putUser($userId) {} should return a 204 No Content on success.

The reasoning:

I've changed the route to be a PUT request to closer follow REST principles. Changing the controller method to putUser from update allows us to better define what the method is intending to do. While it seems trivial, it will help you distinguish between a PUT and PATCH update if you ever decide to implement one in the future. I used PUT as the method here but you can read about PATCH vs PUT and decide how far you want to go into following REST principles.

As for laravels restful controllers I feel they impose restrictions and add no real benefit so for REST api's I don't recommend using them.

Upvotes: 0

Antoine Augusti
Antoine Augusti

Reputation: 1608

If you are building a REST API you should have a URL like example.com/posts/42 and not example.com/posts?id=42 because it is cleaner and it is a coding standard.

I would also drop uppercase characters in your URLs and definitely go for your second choice of implementation. By the way, if you need to update a user you should use a PUT request like so: PUT users/:id.

Upvotes: 1

Related Questions