Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to place business logic is Symfony 2 model layer?

I'm moving my old codebase to the Symfony 2.2 framework.

In my old code, my Article entity has had a method getUrl(), which was returning a URL for current article.

In Symfony i have to use Router service to generate such an URLs.

I can't access Router from inside the Entity, cause it's a bad practice and not really supported by the framework.

I can call the router from the Twig template itself using Twig helper path() and provide all the arguments (from the Article instance) needed to construct the URL. But this approach is not very good, cause if i'll decide to change URL formatting rules - i will have to find all this calls and rewrite them (not very DRY).

I really want to save the business-logic encapsulation here and not to pull all the guts to the view layer.

How should i proceed in this situation?

like image 294
Slava Fomin II Avatar asked Mar 15 '13 12:03

Slava Fomin II


People also ask

Where do you put business logic?

The business logic should be placed in the model, and we should be aiming for fat models and skinny controllers. As a start point, we should start from the controller logic. For example: on update, your controller should direct your code to the method/service that delivers your changes to the model.

Which layer of the application is used to implement the business logic?

The business logic layer is the business components that provide OAGIS services to return data or start business processes. The presentation layer uses these OAGIS services to display data, or to invoke a business process. The business logic provides data required by the presentation layer.

Which layer is business rules written?

In this article, we are going to learn about the Business Logic Layer in Database Management systems. The Business Logic Layer also known as BLL act as an intermediate between the Presentation Layer and the Data Access Layer (DAL). This layer handles the business logic, business rules as well as calculations.

Which is the layer which has most of the business logic built into it?

It's infrastructure layer has logic to handle those commands and queries, most of them probably go to a private database, but we also have some api calls out to the Logistics and Billing domains.


1 Answers

Create an ArticleManager class in your service layer, and handle any business logic there. You can pass the router to it through dependency injection.

For your example, ArticleManager would have a getUrl(Article $article) method which would use the router instance (that you either injected through __construct or a separate setter method) to generate the Url based on properties of $article, and return it.

This method will ensure that your business logic doesn't pollute the view or controller layers.

Be sure to read up on the Service Container docs.

like image 131
Steven Mercatante Avatar answered Sep 20 '22 07:09

Steven Mercatante