John Davidson

php - Unique or Multiple Controllers

0 comments
Message:


I'm doing an app in PHP with a lot of table and I would like to organise properly my code (without framework).


The question is about controllers.


I have tables for person, region, cities, countries, districts...
I don't know if I need a different controller for each Tables (ie: regionController, countriesController...) or a controller that merges some of this tables with the possibility to get different tables entries in the same controller (ie: personController will contain a getCityByPersonId method).


Hope everything is clear.


Thanks a lot !



My Answer:
It ultimately depends on the structure and complexity of your application. Here are a few considerations to help you decide:

1. **Separation of Concerns**: It is generally a good practice to have separate controllers for each entity or resource in your application. This helps in keeping your code organized and makes it easier to maintain and debug in the future.

2. **Single Responsibility Principle**: Each controller should have a single responsibility, which is to handle requests related to a specific entity or resource. Having multiple entities in a single controller can violate this principle and make your code harder to understand.

3. **Reusability**: If you find that certain functionalities are shared across multiple entities (e.g., getting cities by person ID), you can create separate utility classes or services to handle these common functionalities. This way, you can keep your controllers focused on specific entities while still reusing code where needed.

4. **Flexibility**: Having separate controllers for each entity gives you more flexibility in terms of adding new features or making changes in the future. It also makes it easier to scale your application as it grows.

In your case, since you have tables for different entities like person, region, cities, countries, and districts, it might be a good idea to have separate controllers for each of these entities. This will help in keeping your code organized and following best practices in software development.

However, if you find that certain functionalities are shared across multiple entities, you can create utility classes or services to handle these common functionalities and keep your controllers focused on specific entities.

Overall, the decision to have unique or multiple controllers depends on the specific requirements and structure of your application. It's important to consider factors like separation of concerns, single responsibility principle, reusability, flexibility, and scalability when making this decision.

Rate this post

3 of 5 based on 8446 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us