I have the following problem in my laravel 5 project. I have a service provider for form macros named MacroServiceProvider.php. Some macros should receive data from the database, I'm currently using the model and getting the results with eloquent but I want to use repositories instead, so I created my repository but I can't inject this directly to my service provider.
I want something like this:
...
public function register(MyRepoInterface $repo)
{
$registers = $repo->findAll();
Form::macro...
}
...
How can I do this?
Thanks.
I don't think you can do what are you asking, and I think you are misunderstanding the way providers work and what they are intended for.
In providers, you usually say what are the bindings among interfaces and implementations, so that when you do dependency injection in your application code, it works. I'm pretty sure they are not intended for doing real stuff.
For what you say about your code, I imagine something like this:
MyRepoInterface
) with a real implementation using Eloquent
(say EloquentMyRepo
)Macro
, so that you can do Macro::myMacro1()
, Macro::myMacro2()
, etc.myMacro1()
, myMacro2()
, etc, use the repository to get some data from the db and then call some methods from the Form
facadeIf I'm right, then I suggest something like this.
Define the interface in the file MyRepoInterface.php
with
interface MyRepoInterface
{
public function findAll();
// ... your other repo methods
}
and an implementation EloquentMyRepo.php
with
class EloquentMyRepo implements MyRepoInterface
{
public function findAll()
{
// ... do what you need
}
}
Define a facade file MacroFacade.php
with this
use Illuminate\Support\Facades\Facade;
class MacroFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'macro';
}
}
Define your macro service class in a file MacroService.php
, where you can use dependency injection and access your repository. In this class you define your myMacro1()
... methods.
class MacroService
{
protected $myRepo;
public function __construct(MyRepoInterface $myRepo)
{
$this->myRepo = $myRepo;
}
public function myMacro1()
{
// access the repo
$items = $this->myRepo->findAll();
// ... do something with $items and finally return a string
return Form::macro(...);
}
public function myMacro2($arg1, $arg2)
{
// ... use the parameters to do something else
}
}
In your Providers/AppServiceProvider.php
file, go to the register()
method and add
public function register()
{
// ...
$this->app->bind('App\MyRepoInterface', 'App\EloquentMyRepo');
// ...
}
so that when you use MyRepoInterface
in dependency injection, Laravel knows it has to use an instance of EloquentMyRepo
.
Now, let's create a service provider for your macro service. Create a file Providers/MacroServiceProvider.php
and put in it
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class MacroServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind('macro', 'App\MacroService');
}
}
Now, when we need the facade that is registered as macro
, an instance of MacroService
is used.
We finally need some changes to the configuration. Open the config/app.php
file, add the new provider
...
'providers' => [
...
'App\Providers\AppServiceProvider',
...
'App\Providers\MacroServiceProvider',
],
(note that the MacroServiceProvider
is declared after the AppServiceProvider
.)
Add the alias for the facade:
'aliases' => [
...
'Macro' => 'App\MacroFacade',
],
Done!
Let's suppose you call
...
Macro::myMacro1();
...
in your code. How the right method is called?
Macro
is an alias handled by the MacroFacade
classmacro
name by the getFacadeAccessor()
method of MacroFacade
MacroServiceProvider
registered the MacroService
class as an implementation for macro
MacroService
must be created, but it has MyRepoInterface
as dependencyAppServiceProvider
said Laravel to use EloquentMyRepo
when MyRepoInterfice
is requiredEloquentMyRepo
is created and it is used to create an instance of MacroService
Macro
has been resolved to an instance of MacroService
myMacro1()
method of that instanceI hope this can clarify a bit what happens.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With