Laravel - Composer unable to find the service prov

2019-07-05 18:05发布

I have been creating a Laravel package (a Middleware). My folder structure looks like the following:

laravel-censor/
    config/
        sensor.php
    src/
        CensorMiddleware.php
        LaravelCensorServiceProvider.php
    composer.json

And the content of composer.json is:

{
    "name": "kamranahmedse/laravel-censor",
    ...
    ...
    "require": {
        "php" : ">=5.4.0",
        "laravel/framework": "^5.1.11"
    },
    "autoload": {
        "psr-4": {
            "KamranAhmed\\LaravelCensor\\": "src/"
        }
    },
    "minimum-stability": "stable"
}

I have released it to packagist and can be found through github. Now after installing

composer require kamranahmedse/laravel-censor

When I add the package's service provider to providers array in config/app.php

KamranAhmed\LaravelCensor\LaravelCensorServiceProvider::class

And run

php artisan vendor:publish

The problem

It is unable to find the service provider class and throws a class not found exception. Can any body please point out what am I doing wrong here? Why is composer not able to autoload this class?

I have been looking into this for over an hour now and unable to figure out the problem that why composer isn't autoloading this class. Also, I have checked and the namespaces and class names are all correct:

# LaravelCensorServiceProvider.php
namespace KamranAhmed\LaravelCensor;
...
class LaravelCensorServiceProvider extends ServiceProvider
{ ... }

Solution

It turned out that composer had some cache. Running php artisan clear-compiled after clearing the composer cache composer clear did the trick.

1条回答
ゆ 、 Hurt°
2楼-- · 2019-07-05 18:47

I could install your package and execute php artisan vendor:publish without any issues, have you tried composer dumpautoload? and php artisan clear-compiled?

查看更多
登录 后发表回答