0

I have a Facade Class

use App\Http\Response 

class RUser
{
    public function signup(TokenGenerator $token_generator, $data)
    {
        $response = [];
        $access_token = $token_generator->generate(32);
        dd($access_token);
        $user = User::create($data);
        $response['user_id'] = $user->_id;
        $response[''];
    }
}

Now from my UserController I am trying to use the RUser::signup() function like below

class UserController extends Controller
{
    public function store(Request $request)
    {
        $this->request = $request;
        $payload = json_decode($this->request->getContent());
        if($this->validateJson('user.create', $payload)) {
            $validator = Validator::make((array) $payload, User::$rules);
            if ($validator->fails()) {
                $messages = $validator->errors();
                return $this->sendResponse(false, $messages);
            }
            FUser::signup($payload);
            return $this->sendResponse(true, $response);
        }
    }
}

I think Laravel resolves these dependency automatically and i shound not be passing the instance of TokenGenerator explicitly. But i am getting the following error.

Argument 1 passed to App\Http\Responses\RUser::signup() must be an instance of App\Utils\TokenGenerator, instance of stdClass given, called in 
Raheel
  • 8,716
  • 9
  • 60
  • 102

2 Answers2

1

Laravel injects instances in custom classes only in constructors by default. Controller actions will also receive instances injected.

So your example will need such changes to work properly:

class RUser
{
    protected $token_generator;

    public function __construct(TokenGenerator $token_generator)
    {
        $this->token_generator = $token_generator;
    }

    public function signup($data)
    {
        $response = [];
        $access_token = $this->token_generator->generate(32);
        dd($access_token);
        $user = User::create($data);
        $response['user_id'] = $user->_id;
        $response[''];
    }
}

and

class UserController extends Controller
{
    public function store(Request $request, RUser $ruser)
    {
        $this->request = $request;
        $payload = json_decode($this->request->getContent());
        if($this->validateJson('user.create', $payload)) {
            $validator = Validator::make((array) $payload, User::$rules);
            if ($validator->fails()) {
                $messages = $validator->errors();
                return $this->sendResponse(false, $messages);
            }
            $ruser->signup($payload);
            return $this->sendResponse(true, $response);
        }
    }
}

To trigger dependency injection outside when you instantiate class on your own you need to call make() method of Illuminate\Foundation\Application instance, like this:

$fooBar = $this->app->make('FooBar');

See Laravel docs

Ivan Yarych
  • 1,931
  • 17
  • 15
  • Not working :S `Argument 1 passed to App\Http\Responses\RUser::__construct() must be an instance of App\Utils\TokenGenerator, none given, called in called in /var/www/html/myapp/app/Providers/RUserServiceProvider.php on line 29 and defined` – Raheel Mar 24 '16 at 14:33
  • You are probably initializing it calling constructor directly in Service Provider. In such a case dependency injection won't happen. You can create it using `$ruser = $this->app->make('RUser');` construction. – Ivan Yarych Mar 24 '16 at 14:57
  • Where should i do this ? If you are free i actually created a new question with all the codes so that people can know whats going on http://stackoverflow.com/questions/36203271/laravel-di-not-working-when-using-in-facades-underlying-class – Raheel Mar 24 '16 at 15:01
  • `/var/www/html/myapp/app/Providers/RUserServiceProvider.php on line 29` – Ivan Yarych Mar 24 '16 at 15:02
0

I think you are confused with the use of Facades and Dependency Injection. The thing you should follow here is dependence injection upto my knowledge you can use the class directly in here and it will work without any problem TO have a clear idea of when to use facades and when to use dependency injection check this link

Shyam Achuthan
  • 910
  • 5
  • 8