在应用程序文件夹中使用类时如何修复命名空间错误?
我正在将旧版 Laravel 应用程序从 Laravel 5 升级到 8,但遇到了麻烦。我的服务提供商都不起作用,我不明白为什么。
上一个结构
应用程序 -->服务 ------>Stripe
在每个服务提供者文件夹中,我将创建三个文件,如下所示:
- Stripe.php
- StripeFacade.php
- StripeServiceProvider.php
stripe.php
<?php
namespace app\Services\Stripe;
class Stripe
{
}
?>
内 StripeFacade。 ,
<?php
namespace app\Services\Stripe;
use Illuminate\Support\Facades\Facade;
class StripeFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'Stripe';
}
}
在我的 Config/app.php
文件中的StripeServiceProvider.php
中
<?php
namespace app\Services\Stripe;
use Illuminate\Support\ServiceProvider;
class StripeServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton('Stripe', function($app) {
return new Stripe();
});
}
}
我会像这样注册服务提供者和外观:
'providers' => [
app\Services\Stripe\StripeServiceProvider::class,
],
'aliases' => [
'Stripe' => app\Services\Stripe\StripeFacade::class,
]
在我的控制器中,我' d 打电话给条纹然后
use Stripe;
...
public function example(){
$auth = Stripe::auth();
}
我会在 Config/app.php 文件中收到此错误
Class "app\Services\Stripe\StripeServiceProvider" not found
我尝试将 Services 目录添加到我的 psr-4 中,但似乎没有任何运气,即使在转储配置和自动加载。
"autoload": {
"psr-4": {
"App\\": "app/",
"Services\\": "app/Services",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
有什么帮助吗? :)
I am upgrading a legacy laravel application from Laravel 5 to 8 and ran into a brick wall. None of my service providers work, and I cannot figure out why.
Previous Structure
app
-->Services
------>Stripe
Within each service provider folder, I'd create three files like so:
- Stripe.php
- StripeFacade.php
- StripeServiceProvider.php
within stripe.php
<?php
namespace app\Services\Stripe;
class Stripe
{
}
?>
within StripeFacade.php
<?php
namespace app\Services\Stripe;
use Illuminate\Support\Facades\Facade;
class StripeFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'Stripe';
}
}
within StripeServiceProvider.php
<?php
namespace app\Services\Stripe;
use Illuminate\Support\ServiceProvider;
class StripeServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton('Stripe', function($app) {
return new Stripe();
});
}
}
in my Config/app.php
file, I'd register the service provider and facade like so:
'providers' => [
app\Services\Stripe\StripeServiceProvider::class,
],
'aliases' => [
'Stripe' => app\Services\Stripe\StripeFacade::class,
]
In my controller, I'd call the Stripe service as
use Stripe;
...
public function example(){
$auth = Stripe::auth();
}
Then I'd get this error in the Config/app.php
file
Class "app\Services\Stripe\StripeServiceProvider" not found
I tried adding the Services directory to my psr-4 and didn't seem to get any luck, even after dumping configs and autoload.
"autoload": {
"psr-4": {
"App\\": "app/",
"Services\\": "app/Services",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
any help? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的评论中,您发布了一个错误:
我注意到
./app/Services /Stripe
中有一个额外的空格。也许您创建的
Services/
目录末尾有一个空格。一些改进是将
app\
重命名为App\
并从composer.json< 中删除
"Services\\":
行/代码>。在这些更改之后运行composer dump-autoload
。In your comment, you posted an error:
I noticed an extra space in
./app/Services /Stripe
.Perhaps you have created the
Services /
directory with a space at the end.Some improvements are to rename
app\
toApp\
and remove the"Services\\":
line from yourcomposer.json
. Runcomposer dump-autoload
after these changes.使用下面的代码
Use the below code