did you add your service provider on app.php file ?
Hi, I did. I know the service provider file is getting loaded, because if I force a PHP syntax error in there, it gets thrown.
Here's a peak into it:
'providers' => array(
'Illuminate\Foundation\Providers\ArtisanServiceProvider',
'Illuminate\Auth\AuthServiceProvider',
//.....
'lib\ServiceProvider\ExampleClassServiceProvider.php',
),
You're most of the way there.
But when you run new \MyOrg\Examples\ExampleClass
, you're manually initialising the object. Laravel won't intervene here and inject the dependencies.
For Laravel to initialise your object, you need to ask it to.
class ExampleController extends BaseController {
public function __construct(\MyOrg\Examples\ExampleClass $exampleClass)
{
$this->exampleClass = $exampleClass;
}
public function postExample()
{
$this->exampleClass->example_person_create_method();
// ...
}
}
Here we're telling Laravel that when it initialises your controller, it must first initialise your ExampleClass
, which in turns initialises the dependencies for your class.
danharper said:
For Laravel to initialise your object, you need to ask it to.
Here we're telling Laravel that when it initialises your controller, it must first initialise your
ExampleClass
, which in turns initialises the dependencies for your class.
Oh man, duh! Thanks! Slapping myself on this one, that makes so much sense.
I'm getting a Unresolvable dependency resolving [Parameter #0 [ <required> $app ]] in class Illuminate\Support\Manager
error now. I've gotten this earlier in my setup, and I don't remember how I fixed it, but working through trying to fix it now. Any idea?
tlshaheen said:
I'm getting a
Unresolvable dependency resolving [Parameter #0 [ <required> $app ]] in class Illuminate\Support\Manager
error now. I've gotten this earlier in my setup, and I don't remember how I fixed it, but working through trying to fix it now. Any idea?
@danharper Solved this one. Moved my 'lib\ServiceProvider\ExampleClassServiceProvider',
line in app.php providers
from the end of the providers declarations to the beginning; before the Controller gets created. While this is working with no errors, is this technically correct or is there an option/setting I'm not setting?
After you register your class in service provider, you don't need to clone it again with
"new \MyOrg\Examples\ExampleClass"
you just need to call it as Dependency Incejtion like:
// ExampleController.php
use \MyOrg\Examples\ExampleClass;
class ExampleController extends BaseController {
public function postExample(ExampleClass $myClass) {
$personid = $myClass->example_person_create_method();
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community