Where to place business logic in Lumen?

I am developing my first API with Lumen. I usually use services to branch business logic or reusable code from controllers and share with other controllers.

How to do this with clearance? Where to put the services? I only see ServiceProviders for registering these services, but for me it is not clear where and how to define them.

+7
php dependency-injection laravel lumen
source share
2 answers

Services as a class of service? Service classes are not part of the structure, it is more like an application architecture problem that you are trying to solve here.

Depending on the project you are working on, either the Services folder in the application folder (if you send folders by type structure) or the function folder to which it belongs (if you send application folders by function style). These are just two of the many possible ways to create folder structures.

This is different for each project, so you decide where to put the service classes and how you are going to structure your application.

Remember to stick to one agreement throughout the project development cycle. If you can't think about it right now, structure your classes later in refactoring sessions. I usually get a lot more ideas when I work on something else, and not from the very beginning when I think about it.

+3
source share

Lumen and his older brother Laravel come with a service container that handles dependency injections.

To solve the problem from the container, you can either type a hint about the dependency you need in a class that is already automatically resolved by the container, for example, Closure, controller constructor, controller method, middleware, event listener, or queued task. Or you can use the app function from anywhere in the application:

$instance = app(Something::class);

What for the "permission of things." Registering β€œthings” is what service providers are for. A service provider is simply a class that extends Illuminate\Support\ServiceProvider and associates interfaces or classes with specific implementations. (Read the docs for details on how to write your own.)


Example: Create a test route:

$app->get('/test', ' TestController@test ');

and create a controller method, specifying the type of parameter:

 public function test(DatabaseManager $dbm) { dd($dbm); } 

You will see that the DatabaseManager interface is enabled for a particular class, correctly created and configured using the configuration of your DB. This is because, at some point, the infrastructure calls for a service provider who takes care of this.

Any custom providers you want to enable are installed in /bootstrap/app.php as follows:

$app->register(App\Providers\AuthServiceProvider::class);

(Otherwise, if you request a class that has not been connected by the provider, the structure simply introduces a new instance of this class.)


So, for this problem, you probably need some kind of repository class where you can encapsulate all access to the database.

Example:

 // app/Repositories/ProductRepository.php private $db; public function __construct(DatabaseManager $dbm) { $this->db = $dbm->connection(); } public function findById($id) { return $this->db->table('products')->where('id', '=', $id)->get(); } 

 //routes.php $app->get('products/{id}', ' ProductsController@show '); 

 //ProductsController.php public function show(ProductRepository $repo, $id) { $product = $repo->findById($id); dd($product); } 

It is interesting in this example that you are invoking a ProductRepository injection and, since it has a DatabaseManager dependency, the environment processes an instance of both.


I hope this begins to answer your question about managing business logic with service providers. I assume another typical use case is authorization processing. After this introduction, you can follow the documents on this subject .

+1
source share

All Articles