Skip to content

Instantly share code, notes, and snippets.

@adamsafr
Last active March 31, 2023 09:03
Show Gist options
  • Save adamsafr/e73286ba6c631e7e343625498162a32f to your computer and use it in GitHub Desktop.
Save adamsafr/e73286ba6c631e7e343625498162a32f to your computer and use it in GitHub Desktop.
Symfony: ParamConverter usage example
<?php
namespace FrontEndBundle\Request\ParamConverter;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\Common\Persistence\ObjectRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
abstract class AbstractBySlugAndActiveSearcher
{
/**
* @param ParamConverter $configuration
* @param Registry|null $registry
*
* @return bool
*/
final protected function isObjectSupported(ParamConverter $configuration, Registry $registry = null)
{
if ($configuration->getConverter() !== $this->getConverterName()) {
return false;
}
if (!$this->isEntityManagerFound($registry)) {
return false;
}
$className = $configuration->getClass();
if ($className === null) {
return false;
}
$em = $registry->getManagerForClass($className);
if ($em->getClassMetadata($className)->getName() !== $this->getEntityClassName()) {
return false;
}
return true;
}
/**
* @param Registry|null $registry
*
* @return bool
*/
private function isEntityManagerFound(Registry $registry = null)
{
return null !== $registry && count($registry->getManagers()) > 0;
}
/**
* @param ObjectRepository $repository
* @param string $slug
*
* @return object
*
* @throws NotFoundHttpException
*/
protected function getEntity(ObjectRepository $repository, $slug)
{
$entity = $repository->findOneBy([
'slug' => $slug,
'active' => true,
]);
if ($entity === null) {
throw new NotFoundHttpException();
}
return $entity;
}
/**
* @return string
*/
abstract protected function getEntityClassName();
/**
* @return string
*/
abstract protected function getConverterName();
}
<?php
namespace FrontEndBundle\Request\ParamConverter;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use AppBundle\Entity\Category;
class CatalogCategoryParamConverter extends AbstractBySlugAndActiveSearcher implements ParamConverterInterface
{
/**
* @var Registry
*/
protected $registry;
/**
* CatalogCategoryParamConverter constructor.
*
* @param Registry|null $registry
*/
public function __construct(Registry $registry = null)
{
$this->registry = $registry;
}
/**
* {@inheritdoc}
*/
public function apply(Request $request, ParamConverter $configuration)
{
$categorySlug = $request->attributes->get('categorySlug');
if ($categorySlug === null) {
throw new NotFoundHttpException();
}
$repository = $this->registry
->getManagerForClass($configuration->getClass())
->getRepository($configuration->getClass());
$request->attributes->set($configuration->getName(), $this->getEntity($repository, $categorySlug));
}
/**
* {@inheritdoc}
*/
public function supports(ParamConverter $configuration)
{
return $this->isObjectSupported($configuration, $this->registry);
}
/**
* {@inheritdoc}
*/
protected function getEntityClassName()
{
return Category::class;
}
/**
* {@inheritdoc}
*/
protected function getConverterName()
{
return 'catalog_category_converter';
}
}
<?php
namespace FrontEndBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Category;
class CatalogController extends Controller
{
/**
* @ParamConverter("category", class="AppBundle:Category", converter="catalog_category_converter")
*
* @param Request $request
* @param Category $category
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function indexAction(Request $request, Category $category)
{
// ...
}
}
# Param Converters
frontend.param_converter.catalog_category_converter:
class: FrontEndBundle\Request\ParamConverter\CatalogCategoryParamConverter
arguments:
- '@doctrine'
tags:
- { name: request.param_converter, converter: catalog_category_converter }
@davidmpaz
Copy link

Thank you for this piece of wisdom ;)

@cizordj
Copy link

cizordj commented Jul 19, 2022

May the force be with you :-)

@Nugjii
Copy link

Nugjii commented Dec 7, 2022

After the upgrade to symfony 6.2 custom converter does not work. Please help me to find the solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment