Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kjnvalkjnsb's avatar

CRUD Practices

Hey guys I have a question regarding the best way of performing CRUD operations on my models when I may only need a create operation or update for some things...

When data is posted, should I always have a relevant resourceful controller to handle the request or is it ok to have a generic controller that handles a variety of http requests that on a particular method, collects the input and after validating passes the data to a relevant service for parsing and persisting?

Trying to put a finger on the best practice for this sort of stuff.

0 likes
8 replies
bobbybouwmann's avatar

Well it depends on the size of your application. I always like to keep everything organized and therefor I keep every CRUD action on it's own method in a specific controller on that subject. However it's find if you don't want to do that.

kjnvalkjnsb's avatar

That's great, thanks guys.

I was just curious if I should always use a restful controller for any models CRUD operations.

Was curious if passing validated data to a service to be persisteted was a good or bad thing from a controller like for example PageController.

sid405's avatar
sid405
Best Answer
Level 27

@DarrenParker1988 The short answer is 'DEPENDS' on your app. And the long answer is all the responses you got here.

Unless you got further questions, don't forget to mark the thread. :D

Best of luck

thepsion5's avatar

@sid405 When declaring a restful resource, you can use the only key to specify that only some routes should be used, for example:

Route::resource('posts', ['only' => ['create', 'store', 'show'] ]);

Otherwise, your route list will show you routes that don't actually work. Misleading documentation is worse than no documentation.

bobbybouwmann's avatar

@thepsion5 I think @sid405 is trying to do something like this, and where this is a best practise or not

Route::get('something', 'SomeController@something');
Route::get('nothing', 'SomeController@nothing');

As you can see something and nothing are not related to each other like user/create and user/update and therefor they don't belong to each other. But if you have a few of these functions and routes you maybe want to create a single controller to group them instead of creating multiple controllers.

Now I always have a controller for pages, then I can still group stuff while they can be anything. This might work for you as well.

1 like

Please or to participate in this conversation.