Composer의 자동로드 사용
나는이 문제에 대해 운없이 인터넷을 둘러 보았다. 이 코드와 함께 작곡가의 자동로드를 사용하고 있습니다 composer.json
.
"autoload": {
"psr-0": {"AppName": "src/"}
}
하지만 공급 업체 폴더보다 높은 수준에서 자동로드해야합니다.
다음과 같이하면 작동하지 않습니다.
"autoload": {
"psr-0": {"AppName": "../src/"}
}
누구든지 수정 사항을 알고 있거나 어떻게 할 수 있습니까?
모든 패키지는 자체 자동로드를 담당해야합니다. 정의한 패키지에 포함되지 않은 자동로드 클래스로 무엇을 얻으려고합니까?
애플리케이션 자체에 대한 한 가지 해결 방법은 다음과 같이 로더 인스턴스에 네임 스페이스를 추가하는 것입니다.
<?php
$loader = require 'vendor/autoload.php';
$loader->add('AppName', __DIR__.'/../src/');
작곡가 문서는 그 상태 :
autoload 필드를 추가 한 후, vendor / autoload.php 파일을 다시 생성하려면 install을 다시 실행해야합니다.
"src"디렉토리가 "vendor"디렉토리와 같은 수준에 있다고 가정합니다.
- src
- AppName
- 공급 업체
- composer.json
다음 구성은 절대적으로 정확합니다.
{
"autoload": {
"psr-0": {"AppName": "src/"}
}
}
하지만 종속성을 다시 업데이트 / 설치해야 작동합니다. 즉, 다음을 실행합니다.
php composer.phar update
이 명령은 최신 버전의 종속성을 가져오고 구성과 일치하도록 "vendor / composer / autoload_namespaces.php"파일을 업데이트합니다.
또한 @Dom에서 언급했듯이 composer dump-autoload
업데이트를 거치지 않고도 오토로더를 업데이트하는 데 사용할 수 있습니다 .
composer 자동로드 기능을 사용하는 다른 방법도 있습니다. 사용자 정의 자동로드 기능과 함께 제공되는 패키지 또는 네임 스페이스없이 패키지를로드하는 데 유용 할 수있는 방법입니다.
예를 들어 자동로드 기능을 포함하는 단일 파일을 포함하려면 다음과 같이 "files"지시문을 사용할 수 있습니다.
"autoload": {
"psr-0": {
"": "src/",
"SymfonyStandard": "app/"
},
"files": ["vendor/wordnik/wordnik-php/wordnik/Swagger.php"]
},
그리고 Swagger.php
파일 안에 다음 이 있습니다.
function swagger_autoloader($className) {
$currentDir = dirname(__FILE__);
if (file_exists($currentDir . '/' . $className . '.php')) {
include $currentDir . '/' . $className . '.php';
} elseif (file_exists($currentDir . '/models/' . $className . '.php')) {
include $currentDir . '/models/' . $className . '.php';
}
}
spl_autoload_register('swagger_autoloader');
https://getcomposer.org/doc/04-schema.md#files
그렇지 않으면 클래스 맵 참조를 사용할 수 있습니다.
{
"autoload": {
"classmap": ["src/", "lib/", "Something.php"]
}
}
https://getcomposer.org/doc/04-schema.md#classmap
참고 : 테스트 중에 composer dump-autoload
명령 을 실행해야합니다. 그렇지 않으면 변경 사항이 표시되지 않습니다!
./composer.phar dump-autoload
행복한 자동 로딩 =)
제 생각에는 Sergiy의 답변이 주어진 질문에 대해 선택된 답변이어야합니다. 내 이해를 공유하고 있습니다.
아래에 주어진 dir 구조 아래에있는 composer를 사용하여 패키지 파일을 자동로드하려고했습니다.
<web-root>
|--------src/
| |--------App/
| |
| |--------Test/
|
|---------library/
|
|---------vendor/
| |
| |---------composer/
| | |---------autoload_psr4.php
| |
| |----------autoload.php
|
|-----------composer.json
|
내가 사용하고 PSR-4 자동 로딩 사양을.
프로젝트의 composer.json에 아래 줄을 추가해야했습니다. 내 클래스 파일을 src / App, src / Test 및 라이브러리 디렉토리에 배치하려고합니다.
"autoload": {
"psr-4": {
"OrgName\\AppType\\AppName\\": ["src/App", "src/Test", "library/"]
}
}
This is pretty much self explaining. OrgName\AppType\AppName is my intended namespace prefix. e.g for class User in src/App/Controller/Provider/User.php -
namespace OrgName\AppType\AppName\Controller\Provider; // namespace declaration
use OrgName\AppType\AppName\Controller\Provider\User; // when using the class
Also notice "src/App", "src/Test" .. are from your web-root that is where your composer.json is. Nothing to do with the vendor dir. take a look at vendor/autoload.php
Now if composer is installed properly all that is required is #composer update
After composer update my classes loaded successfully. What I observed is that composer is adding a line in vendor/composer/autoload_psr4.php
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Monolog\\' => array($vendorDir . '/monolog/monolog/src/Monolog'),
'OrgName\\AppType\\AppName\\' => array($baseDir . '/src/App', $baseDir . '/src/Test', $baseDir . '/library'),
);
This is how composer maps. For psr-0 mapping is in vendor/composer/autoload_classmap.php
Just create a symlink in your src folder for the namespace pointing to the folder containing your classes...
ln -s ../src/AppName ./src/AppName
Your autoload in composer will look the same...
"autoload": {
"psr-0": {"AppName": "src/"}
}
And your AppName namespaced classes will start a directory up from your current working directory in a src
folder now... that should work.
The autoload config does start below the vendor dir. So you might want change the vendor dir, e.g.
{
"config": {
"vendor-dir": "../vendor/"
},
"autoload": {
"psr-0": {"AppName": "src/"}
}
}
Or isn't this possible in your project?
참고URL : https://stackoverflow.com/questions/12818690/using-composers-autoload
'Development Tip' 카테고리의 다른 글
루비의 문자열에서 작은 따옴표와 큰 따옴표를 이스케이프합니까? (0) | 2020.11.16 |
---|---|
RailwayJS 대 TowerJS (0) | 2020.11.16 |
LINQ to 엔터티에서 LEFT JOIN? (0) | 2020.11.16 |
Visual Studio Code에서 자동 완성 방지 (0) | 2020.11.16 |
ASP.NET 유효성 검사기로 날짜 유효성 검사 (0) | 2020.11.16 |