流明的定制服务提供商
Custom service provider for Lumen
我是 Lumen 的新手,并进行了全新安装 (v8.2.4) 并遵循了文档,尝试编写自己的服务,但我不断收到错误
"Target class [App\Prodivers\BatmanServiceProvider] does not exist."
就像我说的,这是根据 Lumen 文档进行的全新安装。
在 /bootstrap/app.php
$app->register(App\Providers\BatmanServiceProvider::class);
在 /app/Providers/BatmanServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class BatmanServiceProvider extends ServiceProvider
{
public function register()
{
return "batman!";
}
}
我的控制器:app/Http/Controllers/MainController.php
<?php
namespace App\Http\Controllers;
use App\Prodivers\BatmanServiceProvider;
class MainController extends Controller{
public function __construct(BatmanServiceProvider $BatmanServiceProvider){
}
public function main(){
print "hello space!";
}
}
我missing/doing哪里错了?
- 在 /bootstrap/app.php
$app->register(App\Providers\BatmanServiceProvider::class);
- 在 /app/Providers/BatmanServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\BatmanService;
class BatmanServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(BatmanService::class, function(){
return new BatmanService;
});
}
}
- 在 your_lumen_project/app 中创建服务文件夹,并创建 php 文件 BatmanService.php
在 /app/Services/BatmanService.php
<?php
namespace App\Services;
class BatmanService
{
public function sayHello(){
return 'hi, space!';
}
}
- 现在,你可以在任何地方使用!
<?php
namespace App\Http\Controllers;
use App\Services\BatmanService;
class MainController extends Controller{
protected $batmanService;
public function __construct(BatmanService $batmanService){
$this->batmanService = $batmanService;
}
public function main(){
return $this->batmanService->sayHello(); // "hi, space!"
}
}
我是 Lumen 的新手,并进行了全新安装 (v8.2.4) 并遵循了文档,尝试编写自己的服务,但我不断收到错误
"Target class [App\Prodivers\BatmanServiceProvider] does not exist."
就像我说的,这是根据 Lumen 文档进行的全新安装。
在 /bootstrap/app.php
$app->register(App\Providers\BatmanServiceProvider::class);
在 /app/Providers/BatmanServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class BatmanServiceProvider extends ServiceProvider
{
public function register()
{
return "batman!";
}
}
我的控制器:app/Http/Controllers/MainController.php
<?php
namespace App\Http\Controllers;
use App\Prodivers\BatmanServiceProvider;
class MainController extends Controller{
public function __construct(BatmanServiceProvider $BatmanServiceProvider){
}
public function main(){
print "hello space!";
}
}
我missing/doing哪里错了?
- 在 /bootstrap/app.php
$app->register(App\Providers\BatmanServiceProvider::class);
- 在 /app/Providers/BatmanServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\BatmanService;
class BatmanServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(BatmanService::class, function(){
return new BatmanService;
});
}
}
- 在 your_lumen_project/app 中创建服务文件夹,并创建 php 文件 BatmanService.php
在 /app/Services/BatmanService.php
<?php
namespace App\Services;
class BatmanService
{
public function sayHello(){
return 'hi, space!';
}
}
- 现在,你可以在任何地方使用!
<?php
namespace App\Http\Controllers;
use App\Services\BatmanService;
class MainController extends Controller{
protected $batmanService;
public function __construct(BatmanService $batmanService){
$this->batmanService = $batmanService;
}
public function main(){
return $this->batmanService->sayHello(); // "hi, space!"
}
}