Symfony 4 中的私有自定义包在哪里? [英] Where do private, custom bundles live in Symfony 4?
问题描述
我在 Symfony 项目中使用了许多私有的自定义包.在 Symfony 3 下,它们位于 src 的子目录中:
I have a number of private, custom bundles that I use in my Symfony projects. Under Symfony 3, they lived in a sub-directory of src:
src/
DSL/
DSLLibraryBundle/
DSLTelnetBundle/
...
SiteBundle/ # (or AppBundle)
在 Symfony 4 下,特定于应用程序的包消失了,我不清楚我的自定义包应该放在哪里.
Under Symfony 4, the application-specific bundle is gone and it's unclear to me where my custom bundles should live.
关于捆绑包的文档(https://symfony.com/doc/current/bundles/best_practices.html#bundles-naming-conventions) 没有提供放置自定义包的具体建议.
The documentation on bundles (https://symfony.com/doc/current/bundles/best_practices.html#bundles-naming-conventions) provide no specific recommendations for placing custom bundles.
我尝试将 DSL 目录直接放在项目目录和 src/下.无论哪种方式,我都会遇到未定义的类错误.
I have tried placing my DSL directory directly under the project directory and under src/. I end up with undefined class errors either way.
我目前拥有:
src/
DSL/
LibraryBundle/
DSLLibraryBundle.php
捆绑文件:
// src/DSL/DSLLibrary/DSLLibraryBundle.php:
namespace DSL\LibraryBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class DSLLibraryBundle extends Bundle
{
}
bundles.php 中的条目:
The entry in bundles.php:
DSL\LibraryBundle\DSLLibraryBundle::class => ['all' => true],
运行控制台命令时的当前错误:
Current error when running a console command:
PHP 致命错误:未捕获Symfony\Component\Debug\Exception\ClassNotFoundException:试图从命名空间DSL\LibraryBundle"加载类DSLLibraryBundle".
PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "DSLLibraryBundle" from namespace "DSL\LibraryBundle".
一些注意事项:
- 我的自定义包不是通过 Composer 安装的
- 一旦我开始工作,实际的 DSL/目录将是一个符号链接
A couple of notes:
- My custom bundles are not installed via Composer
- The actual DSL/ directory will be a symlink once I get this working
推荐答案
2019 年 4 月 12 日更新:最后,我采取了与最初尝试完全不同的方法.
简而言之,我现在使用 composer 来包含我的自定义包.
Update April 12, 2019:
In the end, I took a completely different approach than my initial attempts.
In a nutshell, I now use composer to include my custom bundles.
我的自定义包位于它们自己的目录树中.
每个包都必须有一个有效的 composer.json 文件来定义包.例如:
My custom bundles live in their own directory tree.
Each bundle must have a valid composer.json file defining the bundle. For example:
{
"name": "dsl/base-bundle",
"description": "Base bundle required by all other DSL bundles",
"type": "symfony-bundle",
"version": "2.1.0",
"license": "proprietary",
"authors": [{"name": "David M. Patterson", "email": "dpatterson@example.com"}],
"minimum-stability": "stable",
"require": {
},
"require-dev": {
},
"autoload": {
"psr-4": {
"Dsl\\BaseBundle\\": "src/"
}
}
}
然后在项目的 composer.json 文件中定义一个自定义仓库:
Then define a custom repository in the project's composer.json file:
"repositories":[
{
"type": "path",
"url": "/full/path/to/DslBaseBundle"
},
], ...
然后让作曲家需要 dsl/base-bundle
Composer 将在 vendor/中创建一个指向包的符号链接,然后一切都按预期工作.
Then do a composer require dsl/base-bundle
Composer will create a symlink in vendor/ to the bundle and everything works as expected from there on.
我的个人库是一个常规的 Symfony 项目,有一个包含我的包的 lib 子目录,每个包都在 lib/下的自己的子目录中.
My personal library is a regular Symfony project with a lib sub-directory that contains my bundles, each in its own sub-directory below lib/.
Symfony 应用程序为我提供了一个方便的测试平台.请注意,自定义包必须与任何其他 Symfony 项目一样包含在其中.
The Symfony application provides me with a convenient test bed. Note that the custom bundles must be included in it the same as for any other Symfony project.
@Stnaire,希望有所帮助.
@Stnaire, hope that helps.
这篇关于Symfony 4 中的私有自定义包在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!