Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

I18N Routing Bundle Laravel Package

benatespina/i18n-routing-bundle

View on GitHub
Deep Wiki
Context7

Doctrine ORM strategy

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
  • For more information about in memory parameters resolver strategy check this cookbook.
  • Back to the index.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui