How do I change the namespace of my application in Laravel?

我与影子孤独终老i 提交于 2021-01-27 12:21:26

问题


I can't change the namespace of my application in Laravel 5.8.

I'm using artisan to change it: php artisan app:name TestApp

Result is: There are no commands defined in the "app" namespace.


回答1:


laravel change this command to app:namespace

you have to do this

php artisan app:namespace TestApp



回答2:


Are you trying to change the namespace of your application or just the name ?

What do you want to change exactly in the namespace ?

EDIT

I think you must have touched something in your application because the command php artisan app:name AppName should work, I just tested it.

Have you ever tried before to change the namespace by yourself?

Otherwise, try composer dump-autoload before to make sure your autoloading is up to date.





回答3:


Looking at php artisan you should have a php artisan app:name NewNamespace command to change the namespace. Make sure that you are on the latest laravel version.

Old answer

To change the namespace of your app you have to edit the composer.json file:

    "autoload": {
        "psr-4": {
            "CustomNamespace\\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ]
    },

but you also have to edit each file in the app subfolders and in the various configurations files (for example in config/app.php, config/auth.php, etc).

After you have done all of that you can run: composer dump-autoload

Keep in mind that this is an error prone method, because if you forget to replace a namespace in any of your files anything can stop working as expected.


Another option would be to create a custom package with the name you want and register it with a custom namespace. For example:

  • Create a lib/yourpackage/src folder in the root of laravel installation
  • Edit the composer.json file to load your custom code:
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "CustomNamespace\\": "lib/yourpackage/src/",
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ]
    },
  • Run composer dump-autoload as before
  • You can now use anywhere your files in your project by referring to the custom namespace you have choosen.



回答4:


I did a fresh Laravel installation and this fixed the issue. I'll post the results once I test @mdexp's updated answer.



来源:https://stackoverflow.com/questions/57545668/how-do-i-change-the-namespace-of-my-application-in-laravel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!