Skip to content

Commit be03e31

Browse files
committed
minor #12456 Getting product using Symfony autowiring (abdounikarim)
This PR was squashed before being merged into the 4.3 branch (closes #12456). Discussion ---------- Getting product using Symfony autowiring Hello, I add a code block to get product using Symfony autowiring and DependencyInjection. Do you think it's useful ? Cheers 😉 <!-- If your pull request fixes a BUG, use the oldest maintained branch that contains the bug (see https://symfony.com/roadmap for the list of maintained branches). If your pull request documents a NEW FEATURE, use the same Symfony branch where the feature was introduced (and `master` for features of unreleased versions). --> Commits ------- 1b92a9f Getting product using Symfony autowiring
2 parents c9dc31a + 1b92a9f commit be03e31

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

doctrine.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,24 @@ be able to go to ``/product/1`` to see your new product::
522522
// in the template, print things with {{ product.name }}
523523
// return $this->render('product/show.html.twig', ['product' => $product]);
524524
}
525+
526+
Another possibility is to use the ``ProductRepository`` using Symfony's autowiring
527+
and injected by the dependency injection container::
528+
529+
// src/Controller/ProductController.php
530+
// ...
531+
use App\Repository\ProductRepository;
532+
533+
/**
534+
* @Route("/product/{id}", name="product_show")
535+
*/
536+
public function show($id, ProductRepository $productRepository)
537+
{
538+
$product = $productRepository
539+
->find($id);
540+
541+
// ...
542+
}
525543

526544
Try it out!
527545

0 commit comments

Comments
 (0)