1
votes

I am using Symfony 2.6

My main bundle is AppBundle, so all namespaces within bundle look like this:

AppBundle\DirectoryName

I would like to change all namespaces so that I end up with something like this:

MyCompanyName\ProjectName\AppBundle\DirectoryName

========================================================

Next text shows what I have tried and the errors I had.

I have class StringUtilities:

<?php

namespace MyCompanyName\ProjectName\AppBundle\Utility;

class StringUtilities {

}

I am using StringUtilities here:

<?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". Did you forget a "use" statement for another namespace?

This is solved by adding this line in app/autoload.php

$loader->addPsr4('MyCompanyName\ProjectName\\', realpath(__DIR__.'/../src'));

Now I tried to change namespace of UseChecker class:

<?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:

[RuntimeException]
The autoloader expected class "AppBundle\Auth\UserChecker" to be defined in file "/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

2
I don't have a complete answer for you. Regarding the Attempted to load class "StringUtilities" from namespace , you will be using StringUtilities inside the class. And not SU . Search and change to SU other than for use statement. - Hari K T
and RuntimeException errors are self explanatory. I think. Look more carefully .. What do you need to change ? - Hari K T
@HariKT SU is used in code and the problem is not there. I have errors like this two in any class of bundle if I try to change namespace. As for RuntimeException from console, I posted all the text of the exception. - Damjan Komlenac
Remove the bundle registered on the AppKernel and fix the changes and add again the bundle. That should probably work. - Hari K T

2 Answers

2
votes

Change your autoload in composer, then run composer dump-autoload.

After that change your namespace bundle and your AppKernel and be happy.

1
votes

I guess that it is not so easy and clean to go around the Sumfony rule:

The bundle directory structure is used as the namespace hierarchy.

So I decided to rename appBundle also. Here is what I did:

  • I made sub folder in src and named it MyCompanyName,
  • and within it I moved AppBundle folder and renamed it to ProjectNameBundle
  • Than I used find replace to change all namespaces

    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. For example, in config.yml i made this change:

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:

app:
    resource: "@AppBundle/Controller/"
    type:     annotation

Into this:

company_management:
    resource: "@ProjectNameBundle/Controller/"
    type:     annotation

The point is: be careful while renaming.