0

I'm developing a package for Laravel and I'm getting an error which I can't figure out how to fix:

Argument 1 passed to Cartalini\Drayman\Drayman::__construct() must be an instance of Cartalini\Drayman\Repositories\UserRepositoryInterface, none given, called in /Applications/MAMP/htdocs/l4/app/controllers/HomeController.php on line 10 and defined

Here's my code...

namespace Cartalini\Drayman;

use Cartalini\Drayman\Repositories\UserRepositoryInterface;

class Drayman
{
    protected $user;

    public function __construct(UserRepositoryInterface $user)
    {
        $this->user = $user;
    }

    public function deliverBeer()
    {
        return $this->user->all();
    }
}

UserRepository...

namespace Cartalini\Drayman\Repositories;

class UserRepository implements UserRepositoryInterface
{
    public function all()
    {
        return User::all();
    }
}

UserRepositoryInterface...

namespace Cartalini\Drayman\Repositories;

interface UserRepositoryInterface
{
    public function all();
}

Service provider...

public function register()
{
    $this->app->bind('Cartalini\Drayman\Repositories\UserRepositoryInterface', 'Cartalini\Drayman\Repositories\UserRepository');
}

And finally my controller...

use Cartalini\Drayman\Drayman as Drayman;

class HomeController extends BaseController 
{
    public function showWelcome()
    {
        $drayman = new Drayman;
        return $drayman->deliverBeer();
    }
}

Can anyone help me to debug this please?

3
  • 4
    Have you tried to read the error message? Commented Nov 26, 2013 at 21:02
  • 1
    The message says it all. Daryamn requires a parameter of the type UserRepositoryInterface but you don't provide one. Commented Nov 26, 2013 at 21:02
  • Follow this and this article for package development in Laravel, I think you missed couple of things. Commented Nov 26, 2013 at 21:34

2 Answers 2

1

In your showWelcome function:

public function showWelcome()
{
    // need to pass a UserRepositoryInterface object here:
    $drayman = new Drayman;
    return $drayman->deliverBeer();
}

Since you did not pass a UserRepositoryInterface object that your code requires you get that error.

Sign up to request clarification or add additional context in comments.

8 Comments

This is not the way Laravel works, it's about Laravel package and this is theoretically right but not right answer in this context.
Do you know how Laravel works ? Check the link I gave in my previous comment.
It doesn't matter what I know but it's a fact that you are not aware of Laravel and what do you answered, how to pass the dependency here, you even didn't give any example, can you make it right ?
What is your advise here to fix the problem, any code example ? You even didn't give the right code according to plain PHP, where did you pass the dependency in your answer ?
^ the advice is on the answer // need to pass a UserRepositoryInterface object here:
|
1

it might be a to late, but i had the same problem and the reason was my concrete class didn't implement it's corresponding interface class. after implementing it, all went well.
although you are correctly implementing it, so this error might have a few reason, which one of them is what i described.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.