Laravel中的命名空间别名

编程入门 行业动态 更新时间:2024-10-23 01:49:27
本文介绍了Laravel中的命名空间别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果这是重复的,我深表歉意;请指导我正确的方向.

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中的命名空间别名

本文发布于:2023-11-09 05:57:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1571565.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:别名   空间   Laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!