2017-06-15 57 views
1

我在我的Windows 7 64Bit Uwamp本地服務器中安裝了Laravel 5.4最新版本。在包中找不到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]'); 

還包括我的服務提供商在配置/ app.php像這樣:

Custom\Timeshow\TimeshowServiceProvider::class, 

但即使在所有這一切,當我跑命令php artisan serve,我得到下面的錯誤:

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

有人能說出什麼是錯在這一點,assit解決呢?

+0

嘗試運行'composer dump-autoload'命令。 – Webinion

+0

只是...做到這一點... php工匠供應商:發佈.. –

+0

很多時候,即使使用'-o'參數,eveytime它說:「生成...成功」,但在相同的錯誤之後。 –

回答

2

我不認爲你可以在你的提供者上調用app-> make()。首先在AppServiceProvier的register()方法中註冊它,如:

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

之後,您應該能夠將其解決出來的應用程序容器。或者,您可以撥打:

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

您的包裹服務提供商。我通常在我編寫的包中綁定單例實例。

編輯

又一想,你的包命名空間添加到您的應用程序composer.json還有:

添加自定義包作曲家
"psr-4": { 
    "Custom\\Timeshow\\": "packages/custom/timeshow/src" 
} 
+0

好的,謝謝,我這樣做,並能夠通過該錯誤,但是當我在瀏覽器中運行以下URL - '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 

and

php artisan optimize