How to configure route instances (Illuminate/Support/Facades/Route.php
) to use already registered services through DI?
For example I register my singleton:
$this->app->singleton('api_controller', function ($app) {
return new Controller('config_value');
});
And controller itself:
class ApiController extends Controller
{
private $configValue;
public function __construct(string $configValue)
{
$this->configValue = $configValue;
}
public function getConfigValue()
{
return $this->configValue;
}
}
Then declaring route:
Route::get('config-value', 'ApiController@getConfigValue')->name('getConfigValue');
But after calling the API, Route initiates new instance of ApiController, and not the one I declared as singleton so config_value is not passed to controller. Is it possible to configure Route instances?
Tired alternate route with app instance, but it does not seem to find api_controller service:
Route::get('config-value', function () { return app('api_controller')->getConfigValue(); })->name('getConfigValue');
In Container.php line 752:
Class api_controller does not exist
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire