Given this code, placed in the default file routes.php in Laravel 5.1:
App::singleton('CatalogsService', function(){
return new CatalogsService;
});
Route::group(['prefix' => 'catalogs'], function() {
Route::get('statesmx', function (){
return var_dump(app('CatalogsService'));
});
Route::get('statesmx2', function() {
return var_dump(app('CatalogsService'));
});
Route::get('statesmx3', function() {
return var_dump(app('CatalogsService'));
});
});
Why am I getting a different instance of CatalogsService when navigating to the 3 defined routes?
localhost/catalogs/statesmx dumps:
object(AppServicesCommonCatalogsService)[152]
localhost/catalogs/statesmx2 dumps:
object(AppServicesCommonCatalogsService)[153]
localhost/catalogs/statesmx3 dumps:
object(AppServicesCommonCatalogsService)[154]
I was expecting to get the same Object Reference Id.
The only way to get the same reference is by adding the next code before the Route::group....
app('CatalogsService');
when I do it the 3 routes dump the same object reference:
object(AppServicesCommonCatalogsService)[106]
I don't get it why is this happening. For me it looks Laravel Singleton is not working properly unless there's something I'm missing about the way a Singleton in Laravel works.
Thank you in advance for any help or hint on this matter.
PD: I'm new to Laravel.
Aucun commentaire:
Enregistrer un commentaire