Support the ongoing development of Laravel.io →
IOC Testing
Last updated 1 year ago.
0

did you add your service provider on app.php file ?

Last updated 1 year ago.
0

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',	
	),
Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

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?

Last updated 1 year ago.
0

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?

Last updated 1 year ago.
0

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();
Last updated 7 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

tlshaheen tlshaheen Joined 14 Oct 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.