2017-06-15 54 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