duangonglian6028 2013-10-16 20:34
浏览 29
已采纳

IoC和接口绑定捕获22

So in trying to implement IoC, DI, etc. in Laravel 4, I've hit a wall. Either I'm misunderstanding something or doing something horribly wrong, not sure which...

So I have a class Person ("business class", not a model or library):

 namespace Entities;
 use Interfaces\Person as PersonInterface;
 class Person implements PersonInterface {...}

a factory which has:

 use Interfaces\Person;
 ...
 App::singleton('user', function($app) {
      ...
      $user_object = new Person();
      ...
 });

and in the aliases array:

 'Interfaces\Person' => 'Entities\Person'

Problem is that doesn't work because the Person class can't implement its interface because the interface is bound back to the Person class:

 Entities\Person cannot implement Entities\Person - it is not an interface 

I seem to be caught in a catch 22 of using IoC and interfaces in the application preventing the classes from actually instantiating.

Don't know if it's relevant, but putting

 App::bind('Interfaces\Person','Entities\Person'); 

in the routes.php files doesn't seem to do anything (but putting it in the aliases array does). Surely I'm doing something wrong here. Any ideas?

  • 写回答

1条回答 默认 最新

  • douzhi1924 2013-10-17 05:30
    关注

    Maybe I can help. To bind an interface to the IoC, you want to have an interface and an implementation of the interface. It looks like you have that step correct. You also want to create a service provider. More info on that here: http://laravel.com/docs/ioc#service-providers

    Remove any bindings you have from the routes.php file. The service provider is what binds the route, and config/app.php registers it in the IoC as described more fully below.

    Your service provider might look something like this:

    File Name: ServiceProviders/PersonServiceProvider.php

    <?php namespace ServiceProviders;
    
    use Illuminate\Support\ServiceProvider;
    use Entities\Person; 
    
    class PersonServiceProvider extends ServiceProvider {
    
    /**
     * Register the binding.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('Interfaces\Person', function()
        {
            return new Person();
        });
    }
    }
    

    Once the service provider is created, register it in the config/app.php file as follows:

    'ServiceProviders\PersonServiceProvider',

    Don't use the aliases. This is used for registering the aliases of facades, which is not what you're attempting to do here if I understand your question correctly.

    Lastly, to follow the generally accepted Laravel naming conventions, I'd suggest naming the interface file "PersonInterface.php" and its interface "interface PersonInterface." Similarly, the implementation file might be called "EloquentPerson.php" and the "class EloquentPerson extends PersonInterface". This assumes you're using Eloquent. It's similar to what you have, but I think the class and interface names could make it more readable with those small tweaks.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟