0

I want to work with Interfaces and Dependency Injection in my Yii 1.1.14 project. Here is the demo code that I have:

The interface:

interface IUserInterface
{   
    public function DoSomething();
}

The class:

class UserService implements IUserInterface
{
    public function DoSomething()
    {
        echo "TEST TEST";
    }
}

Now comes the part that is problematic for me. How do I inject the interface in my controller?

I have tried this:

class AccountController extends Controller
{       
    protected $userService;

    public function __construct(IUserInterface $userInterface) 
    {
        $this->userService = $userInterface;

        parent::__construct();
    }    

    public function actionTest()
    {
        $this->userService->DoSomething();
    }
}

but this won't work, because of CController constructor:

public void __construct(string $id, CWebModule $module=NULL)

What should I do, so I can use the interface in my controller?

I asked the same question on the Yii forum, but we ended up going in circles: http://www.yiiframework.com/forum/index.php/topic/52810-using-interfaces-and-di-in-yii-controllers/

2
  • 1
    In Yii it is better to create your UserService as an ApplicationComponent and use it everywhere like this Yii::app()->userService without DI Commented Mar 27, 2014 at 14:14
  • Now, it can be done easily in Yii 2. Check this : stackoverflow.com/questions/28753235/… Commented Jul 4, 2015 at 13:30

2 Answers 2

1

It's not easy to use dependency injection in Yii since the framework hasn't been created with the idea to use it.

There is one extension letting you use dependency Injection: http://www.yiiframework.com/extension/yiipimple

I havn't tried it so I can't tell you if this extension is answering to your needs.

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

3 Comments

This doesn't look a lot like dependency injection, but rather like service location. You end up fetching things from the container, not injecting them in the controller.
Yeah, it's true. I think you can't perform real dependency injection in Yii whitout modifying the core of the framework. But I think this service locator is the best that you can get in Yii-1. You can have a separation of concerns by using it.
Agreed :) The only way to do otherwise is through extending the framework
1

This is the same problem with Zend Framework 1: the framework uses the constructor so you can't use it for dependency injection.

What I have done to integrate PHP-DI in ZF1 is I override the "Dispatcher" of the framework, that is the object which is responsible to create the controller.

By overriding it, I can control how the controller is created and thus inject dependencies.

Have a look here: https://github.com/mnapoli/PHP-DI-ZF1

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.