2017-06-15 15 views
1

Windows 7 64Bit、UwampローカルサーバーにLaravel 5.4最新vesionをインストールしました。Laravel ServiceProviderクラスがパッケージに見つかりません

私はすでに私はLaravelに登録しようとしています私の "カスタム" のパッケージに、以下のファイルを作成している

パッケージ/カスタム/ timeshow/composer.json:

{ 
    "name": "custom/timeshow", 
    "description": "Show Time in All Timezones", 
    "type": "composer-plugin", 
    "license": "GPL", 
    "authors": [ 
     { 
      "name": "Test Author", 
      "email": "[email protected]" 
     } 
    ], 
    "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" 
    } 
} 

パッケージを\カスタム\ 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'); 
    } 
} 

のパッケージ\カスタム\ 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(); 
    } 
} 

パッケージ\カスタム\ timeshow \ SRC \ routes.phpの

<?php 
Route::get('timeshow/{timezone}', 'Custom\Timeshow\[email protected]'); 

も含まconfigに私のサービスプロバイダー/以下のようなapp.php

Custom\Timeshow\TimeshowServiceProvider::class, 

しかし、たとえすべてこの後、私は実行

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

は、誰かがそれを解決するには、これとassitで間違っているものを指摘することができます:コマンドphp artisan serveは、私は以下のエラーを取得しますか?

+0

'composer dump-autoload'コマンドを実行してみてください。 – Webinion

+0

ちょっと...これは... php artisan vendor:publish .. –

+0

何度も、 '-o'パラメータであっても、"生成されました...成功しました "と言われていますが、同じエラーが発生しました。 –

答えて

2

あなたのプロバイダのapp-> make()をまだ呼び出すことはできません。あなたがアプリケーションコンテナの外にそれを解決することができるはずその後

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

:ようAppServiceProvierのレジスタ()メソッドで最初にそれを登録します。別の方法としては、呼び出すことができます。あなたのパッケージのサービスプロバイダ内

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

を。私は通常、私が書いたパッケージにシングルトンインスタンスをバインドします。

編集

もう一つの思想、同様にあなたのアプリケーションcomposer.jsonにあなたのパッケージの名前空間を追加します。

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

ありがとう、私はそれをして、そのエラーを回避することができましたが、ブラウザでhttp:// localhost:8000/packagetest/public/timeshow/UTCを実行すると、現在の時間をUTCのタイムゾーンに変換したら、それを手伝ってもらえますか? –

+0

あなたはなぜあなたのURLにpackagetest/publicを持っていますか? Webサーバーにドキュメントルートが設定されていますか? – btl

+0

どのようにして通常どのようにlaravelアプリケーションにアクセスしますか?ちょうどhttp:// localhost:8000 /?すべてが正しいなら、パッケージルートはlaravelアプリケーションがとる標準のルーティングパスと似ています。 – btl

0

作曲にカスタムパッケージを追加した後、あなたは、コマンドの下に実行する必要があります

composer dump-autoload 

および

php artisan optimize 
関連する問題