2013-07-11 2 views
12

내 app 라이브러리를 추가하기 위해 app 폴더에 라이브러리 폴더를 만들었습니다. 나는 폴더를 자동로드에 응용 프로그램 구성 파일 및 composer.json을 업데이트했지만, 내가 명령 composer dump-autoload을 실행할 때 다음 오류 얻을 :laravel 4에서 'libraries'를 자동로드하는 방법은 무엇입니까?

{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Class 'App\\Libraries\\Search\\SearchServiceProvider' not found","file":"D:\\Users\\Miguel Borges\\Documents\\Trabalhos\\Tese\\portal\\bootstrap\\compiled.php","line":4130}}PHP Fatal error: Class 'App\Libraries\Search\SearchServiceProvider' not found in D:\Users\Miguel Borges\Documents\Trabalhos\Tese\portal\bootstrap\compiled.php on line 4130 [Finished in 1.1s with exit code 255]

내 응용 프로그램 폴더 트리 :

app 
| ... 
+ libraries 
| + search 
| | - Search.php 
| | - SearchFacade.php 
| | - SearchServiceProvider.php 
| + lib2 
| | - ... 
| + lib3 
| | - ... 
| | - Theme.php 
| - ... 
- filters.php 
- routes.php 

SearchServiceProvider.php

namespace App\Libraries\Search; 

use Illuminate\Support\ServiceProvider; 

class SearchServiceProvider extends ServiceProvider { 

    /** 
    * Register the service provider. 
    * 
    * @return void 
    */ 
    public function register() 
    { 
     $this->app['search'] = $this->app->share(function($app) 
     { 
      return new Search; 
     }); 
    } 

} 

composer.js 기본적으로

"autoload": { 
     "classmap": [ 
      "app/commands", 
      "app/controllers", 
      "app/models", 
      "app/libraries", 
      "app/database/migrations", 
      "app/database/seeds", 
      "app/tests/TestCase.php" 
     ] 
     // , 
    //  "psr-0": { 
    //   "app": "app/libraries" 
    //  } 
    }, 

에, 나는 자동로드에 '라이브러리'폴더 내의 모든 라이브러리가 필요합니다.

+0

''app/libraries ''를 클래스 맵 섹션에 추가 할 수 있습니까? –

+0

이 아니라면, classmap의 폴더는 하위 폴더가 아닌 루트에있는 파일 만로드합니다. 귀하의 회신에 대해 –

답변

23

응용 프로그램의 최상위 네임 스페이스를 만들어야합니다.

그런 다음 모든 라이브러리 을 해당 네임 스페이스 아래에 코드로 지정하십시오. 참고 : 제 3 자 라이브러리는 작성자를 통해 (잘하면) 설치해야하므로 자체 네임 스페이스/자동 로딩 설정이 있어야합니다.

귀하의 디렉토리 구조는 다음과 같다 :

libraries 
    Myapp 
     Search (note directory is capitalized) 
      Search.php 
      SearchFacade.php 
      SearchServiceProvider.php 
     AnotherLib 

그런 다음 클래스 이름 공간 따를 것이다 :

파일 : Myapp/Search/Search.php :

<?php namespace Myapp\Search; 

class Search { ... } 

그리고 마지막으로, 당신의 자동 로딩 설정 :

"autoload": { 
    "classmap": [ 
     "app/commands", 
     "app/controllers", 
     "app/models", 
     "app/libraries", 
     "app/database/migrations", 
     "app/database/seeds", 
     "app/tests/TestCase.php" 
    ] 
    , 
    "psr-0": { 
     "Myapp": "app/libraries" 
    } 
}, 
+0

thx입니다. 이 방법은 작동하지만 파일 구조가 약간 중복 된 것 같습니다. 자동 로딩을 할 수있는 다른 방법이 없습니까? –

+0

처음에는 불필요한 느낌이 들지만, 더 많은 기능을 구현할 때 실제로 경험을 정리하고 정리하는 데 도움이됩니다. –

+0

컨트롤러와 어떻게 작동합니까? 예를 들어, 네임 스페이스 Kitties/Api10이있는 API가 있습니다. 내 컨트롤러에서. 내가 도서관/Kitties/Api10/PoniesLibrary.php 할 PoniesLibrary 네임 스페이스 Kitties/Api10 사용합니까? – Lotus

관련 문제