简体   繁体   中英

Laravel ServiceProvider Class not found in package

I have installed Laravel 5.4 latest vesion in my Windows 7 64Bit, Uwamp local server.

I have already created following files in my "Custom" package that I am trying to register in Laravel:

packages/custom/timeshow/composer.json:

{
    "name": "custom/timeshow",
    "description": "Show Time in All Timezones",
    "type": "composer-plugin",
    "license": "GPL",
    "authors": [
        {
            "name": "Test Author",
            "email": "test1234@gmail.com"
        }
    ],
    "minimum-stability": "dev",
    "require": {},
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/",
            "Custom\\Timeshow\\": "packages/custom/timeshow/src"
        }
    }
    "scripts": {
        "post-install-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "post-create-project-cmd": [
            "php -r \"copy('.env.example', '.env');\"",
            "php artisan key:generate"
        ]
    },
    "config": {
        "preferred-install": "dist"
    }
}

packages\\custom\\timeshow\\src\\TimeshowServiceProvider.php

<?php

namespace Custom\Timeshow;

use Illuminate\Support\ServiceProvider;

class TimeshowServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        include __DIR__.'/routes.php';
        $this->app->make('Custom\Timeshow\TimeshowController');
    }
}

packages\\custom\\timeshow\\src\\TimeshowController.php

<?php
namespace Custom\Timeshow;

use App\Http\Controllers\Controller;
use Carbon\Carbon;

class TimeshowController extends Controller
{
    public function index($timezone)
    {
        echo Carbon::now($timezone)->toDateTimeString();
    }
}

packages\\custom\\timeshow\\src\\routes.php

<?php
Route::get('timeshow/{timezone}', 'Custom\Timeshow\TimeshowController@index');

Also included my service-provider in config/app.php like below:

Custom\Timeshow\TimeshowServiceProvider::class,

But even after all this, when I run the command php artisan serve , I get the below error:

[Symfony\Component\Debug\Exception\FatalErrorException]
  Class 'Custom\Timeshow\TimeshowServiceProvider' not found

Can someone point out what is wrong in this and assit in resolving it ?

I don't think you can call app->make() on your provider just yet. Register it first in the AppServiceProvier's register() method like:

$this->app->register(Custom\Timeshow\TimeshowServiceProvider::class);

After that you should be able to resolve it out of the application container. Alternatively you can call:

$this->app->singleton(Custom\Timeshow\TimeshowServiceProvider::class, function (Application $app) {
    return new TimeshowServiceProvider();
});

within your package service provider. I typically bind singleton instances in the packages I write.

edit

Another thought, add your package namespace to your application composer.json as well:

"psr-4": {
    "Custom\\Timeshow\\": "packages/custom/timeshow/src"
}

after adding custom package to composer you should run below command

composer dump-autoload

and

php artisan optimize

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM