[英]Laravel ServiceProvider Class not found in package
我已在Windows 7 64Bit Uwamp本地服务器中安装了Laravel 5.4最新版本。
我已经在我试图在Laravel中注册的“自定义”包中创建了以下文件:
包/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();
}
}
包\\ 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 dump-autoload
和
php artisan optimize
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.