我在Windows 7 64位,Uwamp本地服务器上安装了Laravel 5.4最新版本。
我已经在我的" Custom"中创建了以下文件我试图在Laravel注册的包:
包/定制/ timeshow / composer.json:
insert_ordered()
包\定制\ timeshow \ SRC \ TimeshowServiceProvider.php
{
"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"
}
}
包\定制\ timeshow \ SRC \ TimeshowController.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 \ routes.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();
}
}
还包括 config / app.php 中的服务提供商,如下所示:
<?php
Route::get('timeshow/{timezone}', 'Custom\Timeshow\TimeshowController@index');
但即便在所有这些之后,当我运行命令Custom\Timeshow\TimeshowServiceProvider::class,
时,我收到以下错误:
php artisan serve
有人可以指出这有什么问题并协助解决吗?
答案 0 :(得分:2)
我认为你还不能在你的提供商上调用app-&gt; 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"
}
答案 1 :(得分:0)
将自定义程序包添加到编辑器后,您应该在命令
下运行composer dump-autoload
和
php artisan optimize