[英]How to change namespace in Symfony2
I am using Symfony 2.6 我正在使用Symfony 2.6
My main bundle is AppBundle, so all namespaces within bundle look like this: 我的主要包是AppBundle,所以bundle中的所有命名空间都是这样的:
AppBundle\\DirectoryName
的appbundle \\目录名
I would like to change all namespaces so that I end up with something like this: 我想更改所有名称空间,以便我最终得到这样的东西:
MyCompanyName\\ProjectName\\AppBundle\\DirectoryName
MyCompanyName \\项目名\\的appbundle \\目录名
======================================================== ================================================== ======
Next text shows what I have tried and the errors I had. 下一个文本显示了我尝试过的内容以及我遇到的错误。
I have class StringUtilities: 我有类StringUtilities:
<?php
namespace MyCompanyName\ProjectName\AppBundle\Utility;
class StringUtilities {
}
I am using StringUtilities here: 我在这里使用StringUtilities:
<?php
namespace AppBundle\Auth;
use MyCompanyName\ProjectName\AppBundle\Utility\StringUtilities as SU;
class UserChecker extends \Symfony\Component\Security\Core\User\UserChecker {
}
When running the application, I am getting this error: 运行应用程序时,我收到此错误:
Attempted to load class "StringUtilities" from namespace "MyCompanyName\\ProjectName\\AppBundle\\Utility".
尝试从命名空间“MyCompanyName \\ ProjectName \\ AppBundle \\ Utility”加载类“StringUtilities”。 Did you forget a "use" statement for another namespace?
您是否忘记了另一个命名空间的“use”语句?
This is solved by adding this line in app/autoload.php 这可以通过在app / autoload.php中添加此行来解决
$loader->addPsr4('MyCompanyName\ProjectName\\', realpath(__DIR__.'/../src'));
Now I tried to change namespace of UseChecker class: 现在我尝试更改UseChecker类的命名空间:
<?php
namespace MyCompanyName\ProjectName\AppBundle\Auth;
use MyCompanyName\ProjectName\AppBundle\Utility\StringUtilities as SU;
class UserChecker extends \Symfony\Component\Security\Core\User\UserChecker {
}
And when I try to clear cache via console (php app/console cache:clear), I get this error: 当我尝试通过控制台清除缓存(php app / console cache:clear)时,我收到此错误:
[RuntimeException]
[RuntimeException的]
The autoloader expected class "AppBundle\\Auth\\UserChecker" to be defined in file "/home/damjan/Temp/licence-management/src/AppBundle/Auth/UserChecker.php".自动加载器期望类“AppBundle \\ Auth \\ UserChecker”在文件“/home/damjan/Temp/licence-management/src/AppBundle/Auth/UserChecker.php”中定义。 The file was found but the class was not in it, the class name or namespace probably has a typo.
找到该文件但该类不在其中,类名或命名空间可能有拼写错误。
And when running the application in browser, I am getting this error: 当在浏览器中运行应用程序时,我收到此错误:
FatalErrorException in UserChecker.php line 0: Compile Error: Cannot redeclare class MyCompanyName\\ProjectName\\AppBundle\\Auth\\UserChecker
UserChecker.php中的FatalErrorException第0行:编译错误:无法重新声明类MyCompanyName \\ ProjectName \\ AppBundle \\ Auth \\ UserChecker
Change your autoload in composer, then run composer dump-autoload
. 在composer中更改自动加载,然后运行
composer dump-autoload
。
After that change your namespace bundle and your AppKernel and be happy. 之后,更改您的命名空间包和您的AppKernel,并感到高兴。
I guess that it is not so easy and clean to go around the Sumfony rule: 我想围绕Sumfony规则并不那么容易和干净:
The bundle directory structure is used as the namespace hierarchy.
bundle目录结构用作命名空间层次结构。
So I decided to rename appBundle also. 所以我决定重命名appBundle。 Here is what I did:
这是我做的:
MyCompanyName
, MyCompanyName
, ProjectNameBundle
ProjectNameBundle
AppBundle -> MyCompanyName\\ProjectNameBundle
AppBundle - > MyCompanyName \\ ProjectNameBundle
Be careful when changing because word AppBundle is not always in class path (namespace), it can be also part of directory path. 更改时要小心,因为单词AppBundle并不总是在类路径(命名空间)中,它也可以是目录路径的一部分。 For example, in config.yml i made this change:
例如,在config.yml中我做了这个改变:
I changed this: 我改变了这个:
orm:
mappings:
CompanyManagementBundle:
type: annotation
dir: %kernel.root_dir%/../src/AppBundle/Entity
prefix: AppBundle\Entity
alias: AppBundle
Into this: 进入:
orm:
mappings:
CompanyManagementBundle:
type: annotation
dir: %kernel.root_dir%/../src/MyCompanyName/ProjectNameBundle/Entity
prefix: MyCompanyName\ProjectNameBundle\Entity
alias: ProjectNameBundle
In routing.yml I changed this: 在routing.yml我改变了这个:
app:
resource: "@AppBundle/Controller/"
type: annotation
Into this: 进入:
company_management:
resource: "@ProjectNameBundle/Controller/"
type: annotation
The point is: be careful while renaming. 关键是:重命名时要小心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.