简体   繁体   English

在包中找不到Laravel ServiceProvider类

[英]Laravel ServiceProvider Class not found in package

I have installed Laravel 5.4 latest vesion in my Windows 7 64Bit, Uwamp local server. 我已在Windows 7 64Bit Uwamp本地服务器中安装了Laravel 5.4最新版本。

I have already created following files in my "Custom" package that I am trying to register in Laravel: 我已经在我试图在Laravel中注册的“自定义”包中创建了以下文件:

packages/custom/timeshow/composer.json: 包/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 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 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 包\\ 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: 还在config / app.php中包含了我的服务提供者,如下所示:

Custom\Timeshow\TimeshowServiceProvider::class,

But even after all this, when I run the command php artisan serve , I get the below error: 但是即使如此,当我运行命令php artisan serve ,也会出现以下错误:

[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. 我认为您还不能在提供程序上调用app-> make()。 Register it first in the AppServiceProvier's register() method like: 首先在AppServiceProvier的register()方法中进行注册,例如:

$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: 再想一想,也将包名称空间添加到应用程序composer.json中:

"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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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