2014-06-09 18 views
11

Cảnh báo: Câu hỏi này là Laravel 4 cụ thể.

Tôi đã sử dụng Facades trong bộ điều khiển của mình trước đây. Vì vậy, tôi biết mã đang hoạt động. Bây giờ tôi cần phải giới thiệu tiêm phụ thuộc vì nhiều lý do khác nhau.

Sau khi sắp xếp các điều khiển tôi nhận được lỗi sau:

Illuminate \ Container \ BindingResolutionException

Unresolvable dependency resolving [Parameter #0 [ $name ]].

tôi không thể tìm ra nơi mà vấn đề là. Thông báo lỗi có vẻ khó hiểu với tôi và tôi không hiểu nó. (Tôi không thấy bất kỳ vấn đề với __constructor thông số của tôi kể từ khi tôi đã đăng ký bắt buộc đối với các HelpersInterface)

Dưới đây là những phần quan trọng của mã của tôi:

File: app/bắt đầu/global.php

<?php 

// ... 

App::bind('Acme\Interfaces\HelpersInterface', 'Acme\Services\Helpers'); 

file: composer.json

// ... 

"autoload": { 
    // ... 
    "psr-0": { 
     "Acme": "app/" 
    } 
}, 

// ... 

File: app/Acme/controllers/BaseController.php

<?php namespace Acme\Controllers; 

use Carbon\Carbon; 
use Controller; 
use Illuminate\Foundation\Application as App; 
use Illuminate\View\Factory as View; 
use Acme\Interfaces\HelpersInterface as Helpers; 
use Illuminate\Http\Response; 

class BaseController extends Controller { 

    /** 
    * @var \Illuminate\Foundation\Application 
    */ 
    private $app; 

    /** 
    * @var \Carbon\Carbon 
    */ 
    private $carbon; 

    /** 
    * @var \Illuminate\View\Factory 
    */ 
    private $view; 

    /** 
    * @var \Acme\Interfaces\HelpersInterface 
    */ 
    private $helpers; 

    function __construct(App $app, Carbon $carbon, View $view, Helpers $helpers) 
    { 
     $this->app = $app; 
     $this->carbon = $carbon; 
     $this->view = $view; 
     $this->helpers = $helpers; 

     $lang = $this->app->getLocale(); 
     $now = $this->carbon->now(); 

     $this->view->share('lang', $lang); 
     $this->view->share('now', $now); 
    } 

    /** 
    * Missing Method 
    * 
    * Abort the app and return a 404 response 
    * 
    * @param array $parameters 
    * @return Response 
    */ 
    public function missingMethod($parameters = array()) 
    { 
     return $this->helpers->force404(); 
    } 

} 

File: app/Acme/Dịch vụ/Helpers.php

<?php namespace Acme\Services; 

use Illuminate\Config\Repository as Config; 
use Illuminate\Database\Connection as DB; 
use Illuminate\Http\Request; 
use Illuminate\Routing\Redirector as Redirect; 
use Illuminate\Session\Store as Session; 
use Illuminate\Support\Facades\Response; 
use Illuminate\Translation\Translator as Lang; 
use Illuminate\View\Factory as View; 
use Acme\Interfaces\MockablyInterface; 
use Monolog\Logger as Log; 

class Helpers implements HelpersInterface { 

// ... 

    public function __construct(
     Config $config, 
     Lang $lang, 
     View $view, 
     MockablyInterface $mockably, 
     Log $log, 
     Request $request, 
     Session $session, 
     DB $db, 
     Redirect $redirect, 
     Response $response 
    ) { 
     // ... 
    } 

// ... 

} 

File: app/Acme/nhà cung cấp /HelpersServiceProvider.php

<?php namespace Acme\Providers; 

use Illuminate\Support\ServiceProvider; 
use Acme\Services\Helpers; 

class HelpersServiceProvider extends ServiceProvider { 

private $db; 
private $defaultDbConnection; 

protected function init() 
{ 
    $this->db = $this->app['db']; 
    $this->defaultDbConnection = $this->db->getDefaultConnection(); 
} 

public function register() 
{ 
    $this->init(); 

    $this->app->bind('helpers', function() 
    { 
     return new Helpers(
      $this->app['config'], 
      $this->app['translator'], 
      $this->app['view'], 
      $this->app['mockably'], 
      $this->app->make('log')->getMonolog(), 
      $this->app['request'], 
      $this->app['session.store'], 
      $this->db->connection($this->defaultDbConnection), 
      $this->app['redirect'], 
      $this->app['Illuminate\Support\Facades\Response'] 
     ); 
    }); 
} 

Trả lời

11

Nó s eems constructor Acme\Services\Helpers của bạn có tham số $name, nhưng không phải là loại được gợi ý.

IoC của Laravel không phải là ma thuật. Nếu bạn không cung cấp gợi ý kiểu cho mọi tham số, vùng chứa IoC sẽ không có cách nào để biết được thứ gì cần chuyển.

+0

Nhờ sự giúp đỡ của bạn Joseph Silber. Tôi đã thêm một số đoạn mã khác để hiển thị rằng tôi đã nhập tất cả mọi thứ. Ngoài ra tôi đã có một 'ServiceProvider' mà nên nói cho IoC container cách thực hiện' HelpersInterface' cụ thể. Tui bỏ lỡ điều gì vậy? – Martin

+0

Tôi chấp nhận câu trả lời của bạn vì bạn đã chính xác. Laravel không biết làm thế nào để khởi tạo dịch vụ người giúp việc. Điều này giúp tôi đi đúng hướng để sửa chữa nó.Tôi đã cung cấp thêm đoạn mã và giải pháp làm việc. Bạn hoặc những người khác có thể quan tâm :-) – Martin

+0

@Martin - Chỉ cần sử dụng tên đầy đủ ('Acme \ Services \ Helpers') làm khóa trong nhà cung cấp dịch vụ của bạn. Bằng cách đó bạn có thể sử dụng 'App :: bind ('Acme \ Interfaces \ HelpersInterface', 'Acme \ Services \ Helpers')' để đăng ký loại bê tông (BTW, bạn nên làm gì trong nhà cung cấp dịch vụ của bạn thông qua '$ this-> app-> bind() '). –

0

Đã khắc phục. Tất cả các hướng dẫn về tiêm phụ thuộc đều đề cập đến việc triển khai cụ thể các giao diện để tôi nghĩ đó là cách để thực hiện nó. Joseph Silber's câu trả lời giúp tôi đi đúng hướng.

Bí quyết là ràng buộc Giao diện với sự ràng buộc của ServiceProvider như được hiển thị bên dưới. Bằng cách đó, Laravel sẽ biết cách khởi tạo dịch vụ Helpers.

File: app/bắt đầu/global.php

<?php 

// ... 

App::bind('Acme\Interfaces\HelpersInterface', 'helpers'); 
Các vấn đề liên quan