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 .