##分からないこと
サービスコンテナの勉強をしている際にweb.phpで試していたのですが、web.phpの$thisからappにアクセスし、様々なメソッドが利用できることが分かりました。
「じゃあこの$thisって何を示しているんだろう」と確認してみたところ、RouteServiceProviderクラスのインスタンスだということ知りました。
何故RouteServiceProviderからappが使用できるのか、という部分に関しては
・Illuminate\Foundation\Bootstrap\RegisterProviders.php
・Illluminate\Foundation\Application.php
・Illuminate\Foundation\ProviderRepository.php
を確認してみて何となく理解はできたのですが、そもそも「何故web.phpでRouteServiceProviderが利用できるのか」、「何故web.phpの$thisはRouteServiceProviderクラスのインスタンスを示すのか」がよく分かりません。
お手数お掛け致しますが、ご教授いただければ幸いです。
よろしくお願いいたします。
##目をつけていた場所
・App\Providers\RouteServiceProvider.phpのbootが起動する時、親のbootを引き継いでいたのでIlluminate\Foundation\Support\Providers\RouteServiceProvider.phpを確認
・親のboot内でloadRoutesというメソッドを発火
php
1 protected function loadRoutes() 2 { 3 //メソッドインジェクションをしているらしい 4 $this->app->call([$this, 'map']); 5 }
・$this(親)を渡しているが、親にmapメソッドはないので継承先(子)のmapメソッドが発火する
→継承先から継承元を辿ることは出来るが、その逆はできない
・子の中で親のbootを発火しているから、この$thisは子自身を示す
・mapメソッド内でmapWebRoutesメソッドを発火
php
1 protected function mapWebRoutes() 2 { 3 Route::group([ 4 'middleware' => 'web', 5 'namespace' => $this->namespace, 6 ], function ($router) { 7 require base_path('routes/web.php'); 8 }); 9 }
・ここまで来てみたけど、「これでweb.phpでRouteServiceProviderクラスのインスタンスが使用できるのか」とはならなかった
##追加
###web.phpの$thisからアクセスできるappのメソッド
サービスコンテナについて勉強していたので、makeやbind、singletonメソッドなどを使用していました。
php
1//web.php 2Route::group(['middleware' => 'auth'], function () { 3 $this->app->bind("something", function() { 4 return "VALUE"; 5 }); 6 Route::get('/home', 'HomeController@index')->name('home'); 7 Route::resource('companies', 'CompanyController'); 8 Route::resource('employees', 'EmployeeController'); 9});
php
1array:102 [▼ 2 0 => "__construct" 3 1 => "version" 4 2 => "bootstrapWith" 5 3 => "afterLoadingEnvironment" 6 4 => "beforeBootstrapping" 7 5 => "afterBootstrapping" 8 6 => "hasBeenBootstrapped" 9 7 => "setBasePath" 10 8 => "path" 11 9 => "basePath" 12 10 => "bootstrapPath" 13 11 => "configPath" 14 12 => "databasePath" 15 13 => "useDatabasePath" 16 14 => "langPath" 17 15 => "publicPath" 18 16 => "storagePath" 19 17 => "useStoragePath" 20 18 => "resourcePath" 21 19 => "environmentPath" 22 20 => "useEnvironmentPath" 23 21 => "loadEnvironmentFrom" 24 22 => "environmentFile" 25 23 => "environmentFilePath" 26 24 => "environment" 27 25 => "isLocal" 28 26 => "detectEnvironment" 29 27 => "runningInConsole" 30 28 => "runningUnitTests" 31 29 => "registerConfiguredProviders" 32 30 => "register" 33 31 => "getProvider" 34 32 => "resolveProviderClass" 35 33 => "loadDeferredProviders" 36 34 => "loadDeferredProvider" 37 35 => "registerDeferredProvider" 38 36 => "make" 39 37 => "bound" 40 38 => "isBooted" 41 39 => "boot" 42 40 => "booting" 43 41 => "booted" 44 42 => "handle" 45 43 => "shouldSkipMiddleware" 46 44 => "configurationIsCached" 47 45 => "getCachedConfigPath" 48 46 => "routesAreCached" 49 47 => "getCachedRoutesPath" 50 48 => "getCachedCompilePath" 51 49 => "getCachedServicesPath" 52 50 => "isDownForMaintenance" 53 51 => "abort" 54 52 => "terminating" 55 53 => "terminate" 56 54 => "getLoadedProviders" 57 55 => "getDeferredServices" 58 56 => "setDeferredServices" 59 57 => "addDeferredServices" 60 58 => "isDeferredService" 61 59 => "configureMonologUsing" 62 60 => "hasMonologConfigurator" 63 61 => "getMonologConfigurator" 64 62 => "getLocale" 65 63 => "setLocale" 66 64 => "isLocale" 67 65 => "registerCoreContainerAliases" 68 66 => "flush" 69 67 => "getNamespace" 70 68 => "when" 71 69 => "resolved" 72 70 => "isAlias" 73 71 => "bind" 74 72 => "addContextualBinding" 75 73 => "bindIf" 76 74 => "singleton" 77 75 => "share" 78 76 => "extend" 79 77 => "instance" 80 78 => "tag" 81 79 => "tagged" 82 80 => "alias" 83 81 => "rebinding" 84 82 => "refresh" 85 83 => "wrap" 86 84 => "call" 87 85 => "factory" 88 86 => "build" 89 87 => "resolving" 90 88 => "afterResolving" 91 89 => "isShared" 92 90 => "getAlias" 93 91 => "getBindings" 94 92 => "forgetInstance" 95 93 => "forgetInstances" 96 94 => "getInstance" 97 95 => "setInstance" 98 96 => "offsetExists" 99 97 => "offsetGet" 100 98 => "offsetSet" 101 99 => "offsetUnset" 102 100 => "__get" 103 101 => "__set" 104]