benatespina/i18n-routing-bundle
This cookbook will try to show how is a basic Doctrine ORM implementation of ParametersResolver. Suppose that we have
a multilingual (of course!) website which contains a CMS section to manage the page contents. For that purpose, we are
using a simple Doctrine ORM Page and PageTranslation entities like the following:
// src/AppBundle/Entity/Page.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* [@ORM](https://github.com/ORM)\Entity
* [@ORM](https://github.com/ORM)\Table(name="page")
*/
class Page
{
/**
* [@ORM](https://github.com/ORM)\Column(type="integer")
* [@ORM](https://github.com/ORM)\Id
* [@ORM](https://github.com/ORM)\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* [@ORM](https://github.com/ORM)\OneToMany(targetEntity="AppBundle\Entity\PageTranslation", mappedBy="page")
*/
private $translations;
public function __construct()
{
$this->translations = new ArrayCollection();
}
// Here the getters and setters...
}
// src/AppBundle/Entity/PageTranslation.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* [@ORM](https://github.com/ORM)\Entity
* [@ORM](https://github.com/ORM)\Table(name="page_translation")
*/
class PageTranslation
{
/**
* [@ORM](https://github.com/ORM)\Column(type="integer")
* [@ORM](https://github.com/ORM)\Id
* [@ORM](https://github.com/ORM)\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* [@ORM](https://github.com/ORM)\Column(type="string", length=255)
*/
private $title;
/**
* [@ORM](https://github.com/ORM)\Column(type="string", length=150)
*/
private $slug;
/**
* [@ORM](https://github.com/ORM)\Column(type="string", length=4)
*/
private $locale;
/**
* [@ORM](https://github.com/ORM)\ManyToOne(targetEntity="AppBundle\Entity\PageTranslation", inversedBy="translations")
* [@ORM](https://github.com/ORM)\JoinColumn(name="page_id", referencedColumnName="id")
*/
private $page;
// Here the getters and setters...
}
The page and page translations are persisted inside database tables so, our implementation of parameters resolver need to do something like this:
// src/AppBundle/Resolver/AwesomeParametersResolver.php
namespace AppBundle\Resolver;
use AppBundle\Entity\PageTranslation;
use BenatEspina\I18nRoutingBundle\Resolver\ParametersResolver;
use Doctrine\ORM\EntityManager;
class AwesomeParametersResolver implements ParametersResolver
{
private $manager;
public function __construct(EntityManager $manager)
{
$this->manager = $manager;
}
public function resolve($fromLocale, $toLocale, array &$parameters)
{
$slug = $parameters['slug'];
$pageTranslation = $this->manager->getRepository(PageTranslation::class)->findOneBy([
'locale' => $fromLocale,
'slug' => $slug
]);
$page = $pageTranslation->getPage();
$translation = $page->getTranslation($toLocale);
if (!$translation) {
throw new \Exception(sprintf(
'Does not exist any translation to the given %s locale',
$toLocale
));
}
$parameters['slug'] = $translation->getSlug();
}
}
Obviously we need to update firstly declared service because in this case our resolver has collaborators.
# app/config/services.yml
services:
app.resolver.my_parameters_resolver:
class: AppBundle\Resolver\AwesomeParametersResolver
arguments:
- "[@doctrine](https://github.com/doctrine).orm.entity_manager"
tags:
-
name: benat_espina_i18n_routing.parameters_resolver
alias: awesome_resolver
How can I help you explore Laravel packages today?