Controllers for Resources, Services and Web Pages in a Symfony Application
The controller processes the request and returns a response. Actions for resource controllers, services, and web pages make up the way you interact with application.
Resource
A resource is an entity that stores data. For example, news, product, order, article, comment. Also, the resource is a list of all units of this entity.
Main actions on the resource:
- Creation (adding to the list)
- Receiving
- Editing
- Removal
Resource API example:
GET /api/catalog/products/30004 # getting the resource "Product" by identifier "30004"
POST /api/blog/articles # creating an "Article" resource with data in the body of the POST request
DELETE /api/blog/comments/1250 # delete resource "Comment" with id "1250"
The segments catalog and blog in the URLs mean that the resources belong to a specific domain and can be separated into the appropriate bundles. For example, the domain-specific resources of a catalog can be Product, Category, Review, and for a blog, the domain-specific resources are Article, Comment, Author.
Service
A service is a feature provided by an application. For example, authorization, text translation or search.
Main actions on the service:
- Performs useful work and returns a response
Services API example:
POST service/security/authorization
POST service/translation/translate
POST service/mass-media/search
Segments in the URL account, translation, mass-media mean belonging to a specific domain or bundle.
Services differ from resources in that they do not have an entity to store data, they only do some work and return a result. For example, authorization, search, text translation.
Web Page
A web page is an HTML representation of resource data and service functions. For example, the main web page may contain the following resources: a menu, a site description, popular products, a list of articles, and the following service functions: authorization and search.
Web pages can display:
- Composition of resources and services (main web page)
- Data of a specific resource (web page of a specific product, article, order)
- List of resource units (list of products, articles, orders)
- Service (authorization web page, search function)
Main actions on a web page:
- Receiving
- Submitting a form with data
Web Pages API Example:
GET /mass-media/news
GET /purchases/orders/2337
GET /account/registration
The segments in the URL mass-media and account mean belonging to a specific domain or bundle.
Controller directory structure:
/Controller
/Api
/Purchases
ApiOrderController.php
/Service
/Security
ServiceAuthorizationController.php
/WebPage
/Purchases
OrderController.php
/Account
RegistrationController.php
Summary
The method of dividing controllers by types - resource, service and web page was considered, the main actions were described, API examples and the structure of the project directory were given.