Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom i18n routing in Symfony

I'm using JMS\I18nRoutingBundle, Gedmo\Translatable and Gedmo\Sluggable. Routes with default locations works as well, but other locales works without translated slug. My i18n routing have following settings:

# Doctrine extensions
stof_doctrine_extensions:
    default_locale: %locale%
    translation_fallback: true
    orm:
        default:
            #…
            sluggable:      true
            translatable:   true
            loggable:       false
            #…

jms_i18n_routing:
    default_locale: cs_CZ
    locales: [cs_CZ, en_US]
    strategy: custom
    hosts:
        cs_CZ: example.cz
        en_US: example.com
    redirect_to_host: true

When I set up route like this:

hw_category:
    pattern: /category/{slug}
    defaults: { _controller: AcmeSiteBundle:Category:detail }
/**
 * @Template
 */
public function detailAction(Category $category)
{}

This routes works
example.cz/category/slug-in-czech
example.com/category/slug-in-czech

But I want to get work example.com/category/slug-in-english which throws 404 exception object not found.

like image 626
kuboslav Avatar asked Oct 21 '22 07:10

kuboslav


1 Answers

In your controller, you have to override method used in entity repository:

/**
 * @Template
 * @ParamConverter(
 *     "category",
 *     class = "AcmeSiteBundle:Category",
 *     options = {
 *         "repository_method" = "findByTranslatedSlug"
 *     }
 *  )
 */
public function detailAction(Category $category)
{}

 

namespace Acme\Bundle\SiteBundle\Entity;

use Doctrine\ORM\EntityRepository;

class CategoryRepository extends EntityRepository
{
    public function findByTranslatedSlug($slug)
    {
        $qb = $this->createQueryBuilder('c')
            ->where('c.slug = :slug')
            ->setParameters($slug);

        $query = $qb->getQuery();
        // set the translation query hint
        $query->setHint(
            \Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
            'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
        );

        return $query->getOneOrNullResult();
    }
}
like image 71
jkucharovic Avatar answered Oct 29 '22 00:10

jkucharovic