简体   繁体   中英

Laravel - Using Controllers

I am using laravel 5.4 and there is one thing i cant understand compeletely. I have used make:controller (name) -resource to create a controller with index/show/create/edit/update/destroy. The app i am making is mostly CRUD operations. My question is :

I must have a separate controller for every entity of my database that needs CRUD operations? For example i have 2 entities : Items, Services. I must have 2 controllers or they can be on the same controller like :

public function store_item(Request $request) {
            **Insert Query**
   }


public function store_service(Request $request) {
            **Insert Query**
   }

What is the correct way to do this?

They can be in the same Controller, but It is Good Practice to Create Different Controller for Items, Services or any CRUD Operations when it comes to Laravel.

In can you just want to create one Controller for both Items and Services, you have to make Functions like store_item and store_service And you have to call them With Every route Like Route::post('items','YourController@store_item'); and Similar for Service.

But if you create Separate Controllers for Items and Services , You Don't have to create Route for Every Task or Action. You can just Register Resourceful Route in Your Routes File and you are good to go!

For example, If you are creating Separate Controllers for both Items and Services, You can just write these two Lines in your Routes File and you are good to go!

Route::resource('items', 'ItemsController');
Route::resource('services', 'ServicesController');

So, It's good to Create Controller for CRUD operations because it makes stuff Neat and Clean in Laravel. Let me know if you have any more queries!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM