0

I have a config file with such array:

'ppr' => [
    'validate' => TestRequest::class
];

Now, I want to retrive this class in other part of the system and use it to validate form (outside of the controller).

While using config('main.ppr.validate') all I receive is namespaced name of the class instead of the class object.

As I already accepted that it won't be that easy to just use reuqests as in controllers, I still do wonder how to pass a class by config. While passing eloquent models it works like a charm (or i.e. config arrays with middlewares etc.), so I suppose there is some magic binding to the IoC to achive that, is it true?

My question is, how to use class passed as in example above without initializing it like:

$obj = new $className;
yivi
  • 42,438
  • 18
  • 116
  • 138
ficus
  • 196
  • 2
  • 17

1 Answers1

3

Laravel (and many other applications) use Dependency Injection to achieve this magic -- your words, not mine! :D

It seems that the Service Container is what handles this in Laravel and should be of help to you.

Directly from the Laravel docs (linked above):

Within a service provider, you always have access to the container via the $this->app property. We can register a binding using the bind method, passing the class or interface name that we wish to register along with a Closure that returns an instance of the class:

$this->app->bind('HelpSpot\API', function ($app) {
    return new HelpSpot\API($app->make('HttpClient'));
});

Also:

You may use the make method to resolve a class instance out of the container. The make method accepts the name of the class or interface you wish to resolve:

$api = $this->app->make('HelpSpot\API');

And:

If some of your class' dependencies are not resolvable via the container, you may inject them by passing them as an associative array into the makeWith method:

$api = $this->app->makeWith('HelpSpot\API', ['id' => 1]);

IMHO, I would look up where/how this is implemented in the native Laravel code (usually the Illuminate vendor) and see how it is used / meant to be implemented.


Furthermore, ClassName::class will return the namespace + class of that class. This is why you only see the class name and are not actually receiving an object/instance of that class.

I'm not sure what/where/why/how you're implementing your class and why you need this functionality somewhere that it doesn't already exist. Laravel is pretty good about already having things set up where you need them, so think twice before breaking out of the box and make sure there isn't a default solution for your situation!

yivi
  • 42,438
  • 18
  • 116
  • 138
domdambrogia
  • 2,054
  • 24
  • 31
  • 1
    I Am aware of service container, however I am still not sure how to make it to work in this particular case, especially dynamically. Anyway, I have some class to which I am passing data. Inside of this class I want to validate it against some rules. Class will decide which rules to use. I already noticed that it won't work so well even if I achive what I want. I guess I will just use a validator manually there and build some custom classes with rules :-) Thank you tho! – ficus Dec 15 '18 at 15:30