Reputation: 1905
I am new with symfony i am trying to use the constructor but it doesn't work and i don't understand why
error:
Cannot autowire service \App\Controller\OutputController argument $product of method &__construct() references class App\Entity\Product but no such service exists.
<?php
namespace App\Controller;
use App\Entity\Product;
class OutputController {
private $product;
public function __construct(Product $product)
{
$this->product = $product;
}
public function jsonFormat() {
return json_encode($this->product->toArray());
}
}
?>
thanks
Upvotes: 0
Views: 4753
Reputation: 464
You use autowiring and you try to autowire a entity. By default they are not exposed as services by this config line from the default app/config/services.yml
:
App\:
resource: '../src/*'
exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'
As you can see, symfony exposes all files from src/*
except from:
Entity,Migrations,Tests,Kernel.php
You should not expose entities as services and you should not rely on entities in your controller, Use services for that
Upvotes: 4