What is the use case of a service Container in laravel? [closed] - php

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I just learned about Laravel Service Containers and it seems like a great functionality.
as I understood it prevents us from rewriting 50 controllers when we should modify a commonly used entity or variable throughout the project.
the problem being is that I don't see a proper Use case for this feature, I mean if you have a piece of data or an entity that you're repeatedly using: this can be customized through a model
so when should I use service containers in laravel ?
what are the Pros and cons of this functionality ?

as I understood it prevents us from rewriting 50 controllers when we should modify a commonly used entity or variable throughout the project.
I do not believe you understand it correctly.
Service Containers are just a fancy term that Laravel came up with to describe dependency injection. The major benefit is for unit testing and its biggest competitor is the facade pattern that Laravel also uses. The biggest benefit of dependency injection is that you can mock expectations without requiring additional "scaffolding" code that bootstraps the test. More information about using dependency injection for unit testing: https://medium.com/philipobenito/dependency-injection-as-a-tool-for-testing-902c21c147f1

Related

Injecting the "Container" in Symfony [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Since Symfony 5.1 injecting the Container by autowiring the Psr\Container\ContainerInterface is deprecated see https://symfony.com/doc/current/service_container.html#public-versus-private-services
In case a service still needs access to the Container, does it needs to be injected by hand for the future, or is there another, more convenient, way to inject it?
One option would be to use Autowiring by Name and inject it, whenever there's a variable called $container, however that feels like a hack.
Question: What is the best practice of giving a service access to the container, if it still depends on it in Symfony 5.1+?
Making your services container-aware has been discouraged for quite a while.
As a general rule, you should avoid making your services container-aware and instead directly pass in the required services in the constructor or via setter-injection. In some cases using a container as a service locator can be useful though. For example when you use the AbstractController or when you have a service that takes in a lot of "registered" services, e.g. for handlers/senders in a Message Bus. For those cases Symfony provides a Service Locator/Subscriber: https://symfony.com/doc/current/service_container/service_subscribers_locators.html
The major difference is, that you have to define the services for the locator/subscriber instead of having all services available, like in Symfony's internal container. You can however tag services that should go into your service locator/subscriber and then collect these tagged services, so they will be passed automatically. See: https://symfony.com/doc/current/service_container/tags.html#reference-tagged-services

How to handle a function with many dependencies [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a method that depends on many other classes like this
public function getProfileData(
ProfilesService $profile_service,
ContactInfoService $contact_info_service,
CoursesService $courses_service,
InterestsService $interests_service,
LanguagesService $languages_service,
PersonalInfoService $personal_info_service,
ProjectsService $projects_service,
SkillsService $skills_service,
AwardsService $awards_service,
EducationsService $education_service,
ExperiencesService $experiences_service,
TargetJobsService $target_jobs_service,
ProfileHiddenSectionsService $hidden_sections_service) { }
I read about dependency injection and I know that if you exceed 6-10 dependencies it leads you to a code smell.
But this getProfileData() method really need all of these dependencies so what is the best practice to solve this problem ?!
The code smell you are experiencing is called Constructor over-injection (and this particular variation is Method over-injection). As #Nkosi said in the comments, the source of this is a Single Responsibility Principle violation.
How to solve this problem, however, depends pretty much on the situation. Chapter 6 from the book Dependency Injection: Principles, Practices, and Patterns actually contains a very elaborate description of your options. In short, among other things, you can use the following refactorings:
Facade Services
Introduce Domain Events
Hide similar components behind a Composite
Extract cross-cutting concerns using Decorators

Symfony 2 architecture - best practices [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Now, im facing three issues about my symfony 2 application architecture using Doctrine 2. This is a one bundle application.
I will use some controllers of course, but I need some special controller "BeforeController" to be called before the others. In the "BeforeController" I want to place some initialize methods. Is it best practice to extends controllers by "BeforeController"?
I want to create some services in ServiceContainer. For every entity which I have, I want to create own service (e.g. There is Products entity, so I will create Product service, which provides methods manipulating with products and so on), if is it good way. Or is it in Controller compentence?
Can somebody explain me, what is in controller or in service competence?
I should like to create some logical and intuitive architecture. Maybe any class diagram should help me.
Can somebody describe me some best practices about it?
From documentation:
kernel.controller Event, and example: Before filters with the kernel.controller Event,
Service: Service base info and more: What is a Service? Controller base info, and additionally Action. It's nothing wrong with creating separate services for each entity if you really need it.
As above.
I recommended this video from SymfonyCon Warsaw 2013 How Kris Writes Symfony Apps.
Hope this help.

Why should I use MVC in a php website? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Many frameworks are based on mvc, but I have no idea about MVC. So what is the use of MVC?
Is it for speed, security or any other reason?
MVC allows you to separate your business logic from your presentation layer. This "Separation of Concerns" allows you to quickly find and edit portions of your code. It also enables easy reuse of your UI components across your system.
Check out the wiki page for a overly academic and technical introduction to MVC http://en.wikipedia.org/wiki/Model_view_controller
MVC is mostly for better maintainability of your code. By separating the database logic from the presentational logic from the controller logic you can make changes/rewrites/maintence more easily.
it also solves the problem of "spaghetti code", you can outsorce your HTML/XML/PDF/XSL creation Code to your View/Template engine, get the Data from Your Model(DB/File/RemoteCall,...) and your Controller controls the behaviour of Both, you can also simply exchange the View/Models without even Change the Controller if u implement it right, so you gain Seperation of Concerncs, get better Code & Maintainability and can easily swap components
also its easier to manage if your projects grow. I recommend the Usage of a FrontController which selects the right Controller for you depending on the Users input, you can also use Inversion of Control/DependencyInjection Pattern there and let your Controller be configued by your FrontController / Pass DB Connection and lots of lots of lots more funny stuff
Now u got a simple application framwork:) Use Zend Instead:)

in MVC, where do you draw the line between a controller and model? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I've seen code written where almost all non-route related code is passed to a model. I have also seen code where all database persistence is handled by a model, but non-DB processing is handled by the controller.
Which is the better approach?
The line between controller and model is actually quite clear.
Model is your application's heart. It contains the business/domain logic required to solve the problem your application was written for. The Model is usually layered into several other layers, e.g. persistence, services, domain, etc. It's a common misconception that the Model is just the database, as much as it is a common misconception that the database should be an ActiveRecord.
The controller (and the view) are part of the presentation layer. A controller's sole responsibility is to receive and handle user input directed towards your application and delegate this to the appropriate parts in the model. Nothing more. It should not handle complex application flow or code of your problem domain. You want controllers to be skinny and models fat with logic. The Model should not know about either C or V and you should be able to swap out V and C for a different presentation layer without having to touch your M.
See MVC Excerpt in Patterns of Enterprise Application Architecture

Categories