テストコンテナを作成する流れ
- [ ]
Route::get('/niniNoRoute', [NiniNoControllerName::class, 'niniNoMethodName']);
と、ファイル上部へ
use App\Http\Controllers\NiniNoControllerName;
を入力。
- [ ]
php artisan make:controller TestController
[ ]
app/Http/Controllers 配下 へ、設定したメソッドを追記
class TestController extends Controller { public function testMethod(){ } }
- [ ]
class TestController extends Controller { public function testMethod() { dd(app()); } }
で、サーバーを立ち上げ、設定したルートでアクセスすると、黒画面でこんなのが出てくる。これが「サービスコンテナ」。Laravelのコア機能群。
Illuminate\Foundation\Application {#2 ▼ #resolved: array:54 [▶] #bindings: array:66 [▶] #methodBindings: [] #instances: array:42 [▶] #scopedInstances: [] #aliases: array:77 [▶] #abstractAliases: array:40 [▶] #extenders: array:1 [▶] #tags: [] #buildStack: & [] #with: & [] +contextual: [] #reboundCallbacks: array:2 [▶] #globalBeforeResolvingCallbacks: [] #globalResolvingCallbacks: [] #globalAfterResolvingCallbacks: [] #beforeResolvingCallbacks: [] #resolvingCallbacks: array:1 [▶] #afterResolvingCallbacks: array:3 [▶] #basePath: "/Applications/MAMP/htdocs/ipps/develop" #hasBeenBootstrapped: true #booted: true #bootingCallbacks: & array:2 [▶] #bootedCallbacks: & array:1 [▶] #terminatingCallbacks: [] #serviceProviders: & array:26 [▶] #loadedProviders: array:26 [▶] #deferredServices: array:122 [▶] #appPath: null #databasePath: null #langPath: null #storagePath: null #environmentPath: null #environmentFile: ".env" #isRunningInConsole: false #namespace: null #absoluteCachePathPrefixes: array:2 [▶] }
※キャッシュがきついので事前に消すのが吉
- [ ]
class TestController extends Controller { public function testMethod() { app()->bind('testTest', function () { return 'テストのコンテナです'; }); dd(app()); } }
[ ]
※基本は1ファイル1class
class TestController extends Controller { public function testMethod() { app()->bind('testTest', function () { return 'テストのコンテナです'; }); $test = app()->make('testTest'); dd($test, app()); } }
- [ ]
php artisan make:provider SampleServiceProvider