Symfony @ParamConverter: exclude use if placeholder does not contain dots

I have this controller:

/**
 * {@inheritdoc}
 *
 * @Route("entity/{domain_host}")
 * @ParamConverter("entity", class="AppBundle:Entity", options={
 *     "repository_method" = "findOneByDomainHost",
 *     "mapping": {"domain_host": "domainHost"},
 *     "map_method_signature" = true
 * })
 */
class EntityController extends Controller
{
...
}

Thus, a similar URL http://example.com/entity/another-example.comcorresponds to the action, and the corresponding object is another-example.comhydrated and passed to the controller.

Now this object also has an identifier.

I would like to intercept the url, for example http://example.com/entity/12345, and redirect it to http://example.com/entity/another-example.com.

For this, I created another method in EntityController:

/**
 * {@inheritdoc}
 *
 * @Route("entity/{domain_host}")
 * @ParamConverter("store", class="AppBundle:Entity", options={
 *     "repository_method" = "findOneByDomainHost",
 *     "mapping": {"domain_host": "domainHost"},
 *     "map_method_signature" = true
 * })
 */
class EntityController extends Controller
{
    public function redirectIdToDomainAction(Request $request)
    {
        die(dump($request));
        // Following will be the redirect logic
    }
}

And in mine routing.yml, AT THE BEGINNING OF THE FILE:

entity_id_to_domain:
    path: /entity/{id}
    defaults: { _controller: AppBundle:Entity:redirectIdToDomain }
    requirements:
        id: ^[^.]*$
    methods: [GET]

In practice, the action redirectIdToDomainis called if the placeholder does not contain a point (the point is discriminant: if the placeholder has a point, the domain is transferred, if there is no point, then probably the placeholder represents Entity by uts ID, and I should redirect).

, , EntityController @ParamConverter, - , , a AppBundle:Entity object not found..

, @ParamConverter , ? , , redirectToIdAction Not found?

+4
1

ParamConverter , , .

1

, , , .

/**
 *@Route(
    "entity/{domain_host}",
     name="entity_domain",
     requirements={"domain_host": "^(?=.*[\w])(?=.*[.]).+$"}
 *)
 *@ParamConverter(...)
 */
someActionOnDomainAction()
{
//...
}


/**
 *@Route(
    "entity/{id}",
     name="entity_domain",
     requirements={"id": "^[\d]$"}
 *)
 */
redirectIdToDomainAction()
{
//...
}

2

findOneByDomainHost() findOneByDomainHostOrID() , .

Object Not Found, .

:

/**
 * Class EntityController
 *
 * @Route("/entity/{domain_host}", name="domain_host")
 * @ParamConverter("domain_host", class="AppBundle:Entity", options={
 *     "repository_method" = "findOneByHostOrID"
 * })
 */
class EntityController extends Controller
{
    /**
     * @Route("/", name="show_domain_host")
     * @param Entity $domain_host
     */
    public function domain_hostAction(Entity $domain_host, Request $request)
    {
        $id = $domain_host->getId();
        $host = $domain_host->getHost();

        // Get the unconverted route parameter.
        $parameter = $request->attributes->get('_route_params')['domain_host'];

        if ($parameter == $domain_host->getId()){
            // If the route parameter matches the ID,
            // redirect to the same route using the domain host.
            $url = $this->generateUrl('show_mail', ['mail' => $lib]);
            return $this->redirect($url, 301);
        }

        return new Response("<html><body>$id $host</body></html>");
    }
}

:

class EntityRepository extends EntityRepository
{
    public function findOneByHostOrID($domain_host)
    {
        if (preg_match("[\\d]",$domain_host)) {
            return $this->findOneById($domain_host);
        } else {
            return $this->findOneByHost($domain_host);
        }
    }
}
+1

Source: https://habr.com/ru/post/1662140/


All Articles