Laravel ServiceProvider Class 未在包中找到
Laravel ServiceProvider Class not found in package
我已经在我的 Windows 7 64 位 Uwamp 本地服务器中安装了 Laravel 5.4 最新版本。
我已经在我的 "Custom" 包中创建了以下文件,我正尝试在 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');
还在 config/app.php 中包含我的服务提供商,如下所示:
Custom\Timeshow\TimeshowServiceProvider::class,
但即便如此,当我 运行 命令 php artisan serve
时,我仍收到以下错误:
[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'Custom\Timeshow\TimeshowServiceProvider' not found
谁能指出其中的错误并协助解决?
我认为您现在还不能在您的提供商上调用 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"
}
将自定义包添加到 composer 后,您应该 运行 在命令下方
composer dump-autoload
和
php artisan optimize
我已经在我的 Windows 7 64 位 Uwamp 本地服务器中安装了 Laravel 5.4 最新版本。
我已经在我的 "Custom" 包中创建了以下文件,我正尝试在 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');
还在 config/app.php 中包含我的服务提供商,如下所示:
Custom\Timeshow\TimeshowServiceProvider::class,
但即便如此,当我 运行 命令 php artisan serve
时,我仍收到以下错误:
[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'Custom\Timeshow\TimeshowServiceProvider' not found
谁能指出其中的错误并协助解决?
我认为您现在还不能在您的提供商上调用 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"
}
将自定义包添加到 composer 后,您应该 运行 在命令下方
composer dump-autoload
和
php artisan optimize