如果这是重复的,我深表歉意;请指导我正确的方向.
I apologize if this is duplicate; please guide me to the right directions.
我知道我们可以从/config/app.php在Laravel中创建类别名.但是,尝试使用相同的方法创建名称空间别名失败.
I know we can create class aliases in Laravel from /config/app.php. However trying to create namespace aliases using the same method fails.
/* |-------------------------------------------------------------------------- | Class Aliases |-------------------------------------------------------------------------- | | This array of class aliases will be registered when this application | is started. However, feel free to register as many as you wish as | the aliases are "lazy" loaded so they don't hinder performance. | */ 'aliases' => [ ... /* * My custom namespace aliases */ 'MyNamespace' => 'App\Models\MyNamespace', ],在思想器中对其进行测试将返回以下结果:
Testing this in thinker returns the following results:
>> new \MyNamespace\MyClass(); PHP Fatal error: Class 'MyNamespace/MyClass' not found in Psy Shell code on line 1
您知道在Laravel中创建名称空间别名的方法吗?
Do you know a way to create namespace alias in Laravel?
推荐答案使您的代码PSR-4兼容并将其放入composer.json
Make your code PSR-4 compatible and put it in composer.json
"autoload": { "psr-4": { "MyNamespace\\": "src/MyNameSpace", } },然后运行composer dumpautoload.只要您遵循子文件夹即名称空间的约定,您的类就会自动加载.
Then run composer dumpautoload. So long as you stick to the convention of subfolders being namespaces, your classes will autoload.
src/MyNamespace/SomeClass.php将具有namespace MyNamespace;和class SomeClass.
src/MyNamespace/Something/SomethingElse.php将具有namespace MyNamespace\Something;和class SomethingElse.
更多推荐
Laravel中的命名空间别名
发布评论