I have problem with service container in Symfony 5. I have basic class CoreModel and I want use it in base controller, that everyone has access to it.
namespace App\Model;
use App\Model\Core\CodeModel;
class CounterModel
{
}
My call in controller:
$this->get('model.counter');
I added to services.yml this connection
services:
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Utils,Service,Model,Entity,Migrations,Tests,Form, Validatior, Kernel.php}'
model.counter:
class: App\Model\CounterModel
and i have all the time error.
Service "model.counter" not found: even though it exists in the app's container, the container inside "App\Controller\CountersController" is a smaller service locator that only knows about the "doctrine", "form.factory", "http_kernel", "message_bus", "messenger.default_bus", "parameter_bag", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage", "serializer", "session" and "twig" services. Try using dependency injection instead.
I have no idea what to do wrong. Adding CounterModel in constructor controller is ok. But I don't care about such a solution.
try to make your service model.counter
public:
services:
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Utils,Service,Model,Entity,Migrations,Tests,Form, Validatior, Kernel.php}'
model.counter:
class: App\Model\CounterModel
public: true # <--- here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.