2013-03-24 4 views
6

아래 코드는 모든 것을 말해 할 수 있도록 데이터를 전달Laravel 4 : ... 서비스 제공자

당신은 서비스 제공자의 내부 클래스에 테스트 배열을 전달해야
// routes.php 
App::make('SimpleGeo',array('test')); <- passing array('test') 

// SimpleGeoServiceProvider.php 
public function register() 
{ 
    $this->app['SimpleGeo'] = $this->app->share(function($app) 
    { 
     return new SimpleGeo($what_goes_here); 
    }); 
} 

// SimpleGeo.php 
class SimpleGeo 
{ 
    protected $_test; 

    public function __construct($test) <- need array('test') 
    { 
     $this->_test = $test; 
    } 
    public function getTest() 
    { 
     return $this->_test; 
    } 
} 
+1

안녕하십니까 @schmaltz 귀하의 질문에 대한 답변을 얻었습니까? 내 응용 프로그램이 당신과 비슷한 아키텍처를 사용하므로 같은 문제가 발생하여 솔루션을 찾고 있습니다. – Omranic

답변

3

// NOT in routes.php but when u need it like the controller 
App::make('SimpleGeo'); // <- and don't pass array('test') 

public function register() 
{ 
    $this->app['SimpleGeo'] = $this->app->share(function($app) 
    { 
     return new SimpleGeo(array('test')); 
    }); 
} 

YourController.php

Public Class YourController 
{ 
    public function __construct() 
    { 
     $this->simpleGeo = App::make('SimpleGeo'); 
    } 
} 
+0

예, 효과가있는 것으로 나타났습니다. 내 가치는 정적이 아니며 어떻게 든 통과해야합니다. 예를 들어 Route in Input에서 사용할 수 있습니다. –

+0

죄송합니다. 이게 뭔가있을 수도 있습니다. http://stackoverflow.com/questions/15483542/laravel-4-way-to-inject-an-object-that-requires-configuration-into-a-controller –

7

당신이 클래스를 결합하는 시도 할 수 있습니다 매개 변수를 앱 컨테이너에 직접 입력하십시오 (예 :

)
<?php // This is your SimpleGeoServiceProvider.php 

use Illuminate\Support\ServiceProvider; 

Class SimpleGeoServiceProvider extends ServiceProvider { 

    public function register() 
    { 
     $this->app->bind('SimpleGeo', function($app, $parameters) 
     { 
      return new SimpleGeo($parameters); 
     }); 
    } 
} 

SimpleGeo.php를 그대로 둡니다. 귀하의 routes.php에서 테스트 할 수 있습니다

$test = App::make('SimpleGeo', array('test')); 

var_dump ($test); 
+0

내가 앱을 할 경우 :: 당신처럼 bind하기 App이 정의되지 않았다. 만약 내가 그것을 유지하지만 두 번째 매개 변수를 추가 할 $ 매개 변수 경고 : 누락 된 인수 2 –

+0

그것은 이상한, 나를 위해 정상적으로 작동합니다. 나는 ** SimpleGeo.php **와 ** SimpleGeoSeriveProvider.php **를 __/libraries__ 폴더에 넣었다. (그리고 composer.json 파일에 경로 로더를 추가 한 다음 ** composer dump-autoload ** 명령을 내렸다.) ** 'SimpleGeoServiceProvider'**를 __app/config/app.php__의 공급자 배열에 추가했습니다. Btw, SimpleGeoServiceProvider.php 또는 routes.php에서 발생한 오류입니까? –

+0

글쎄, 나는 "workbench"명령으로 광산을 건설했다. 그래서 그것은 workbench /에있다. SimpleGeoServiceProvider에서 오류를 throw합니다. 나는 두 번 확인하고 다시 당신에게 돌아갈거야. –