2

Les URI avec des caractères spéciaux allemands ne fonctionnent pas (erreur 404). J'ai déjà eu ce problème (here) et il a été résolu avec le unicode modifier et un custom view helper, qui l'utilise.Les caractères spéciaux ne sont pas analysés correctement dans Zend Framework 2

Maintenant, j'ai le même problème avec une route enfant Segment, mais cette fois l'approche avec l'identificateur unicode et une aide de vue personnalisée ne fonctionne pas.

Toutes les demandes comme sld.tld/sport/sportäöüÄÖÜß/cityäöüÄÖÜß ou sld.tld/sport/sportäöüÄÖÜß/cityäöüÄÖÜß/page/123 se terminent par une erreur 404.

/module/Catalog/config/module.config.php

<?php 
return array(
    ... 
    'router' => array(
     'routes' => array(
      'catalog' => array(
       ... 
      ), 
      'city' => array(
       ... 
      ), 
      // works correctly, if I remove the child route 
      'sport' => array(
       'type' => 'MyNamespace\Mvc\Router\Http\UnicodeRegex', 
       'options' => array(
        'regex' => '/catalog/(?<city>[\p{L}\p{Zs}]*)/(?<sport>[\p{L}\p{Zs}]*)', 
        'defaults' => array(
         'controller' => 'Catalog\Controller\Catalog', 
         'action'  => 'list-courses', 
        ), 
        'spec' => '/catalog/%city%/%sport%', 
       ), 
       'may_terminate' => true, 
       'child_routes' => array(
        'courses' => array(
         'type' => 'segment', 
         'options' => array(
          'route' => '[/page/:page]', 
          'defaults' => array(
           'controller' => 'Catalog\Controller\Catalog', 
           'action'  => 'list-courses', 
          ), 
         ), 
         'may_terminate' => true, 
        ), 
       ) 
      ), 
     ), 
    ), 
    ... 
); 

J'ai aussi essayé avec un itinéraire enfant UnicodeRegex:

 'sport' => array(
      'type' => 'MyNamespace\Mvc\Router\Http\UnicodeRegex', 
      'options' => array(
       'regex' => '/catalog/(?<city>[\p{L}\p{Zs}]*)/(?<sport>[\p{L}\p{Zs}]*)', 
       'defaults' => array(
        'controller' => 'Catalog\Controller\Catalog', 
        'action'  => 'list-courses', 
       ), 
       'spec' => '/catalog/%city%/%sport%', 
      ), 
      'may_terminate' => true, 
      'child_routes' => array(
       'courses' => array(
        'type' => 'MyNamespace\Mvc\Router\Http\UnicodeRegex', 
        'options' => array(
         'regex' => '/page/(?<page>[\p{N}]*)', 
         'defaults' => array(
          'controller' => 'Catalog\Controller\Catalog', 
          'action'  => 'list-courses', 
         ), 
         'spec' => '/page/%page%', 
        ), 
        'may_terminate' => true, 
       ), 
      ) 
     ), 

UnicodeRegex

voir here

UnicodeSegment

Prolonge Zend\Mvc\Router\Http\Segment et complète l'entrée de tous les appels preg_match(...) avec u:

Comment le faire fonctionner?

+0

J'ai déposé ce [comme un bug dans le numéro ZF2 trackeg] (https://github.com/zendframework/zf2/issues/7335). Un abonnement là-bas pourrait aider à corriger cela en amont. – Caleb

Répondre

3

Juste jeté un coup d'oeil à ceci, vous devez changer la méthode de correspondance UnicodeRegex afin qu'il renvoie la longueur correcte pour la partie de l'URL qu'il correspondait, voici une tentative de résoudre ce problème, qui semble fonctionner (au moins pour moi) avec votre configuration

public function match(Request $request, $pathOffset = null) 
{ 
    if (!method_exists($request, 'getUri')) { 
     return null; 
    } 

    $uri = $request->getUri(); 
    $path = rawurldecode($uri->getPath()); 

    if ($pathOffset !== null) { 
     $result = preg_match('(\G' . $this->regex . ')u', $path, $matches, null, $pathOffset); 
    } else { 
     $result = preg_match('(^' . $this->regex . '$)u', $path, $matches); 
    } 

    if (!$result) { 
     return null; 
    } 

    foreach ($matches as $key => $value) { 
     if (is_numeric($key) || is_int($key) || $value === '') { 
      unset($matches[$key]); 
     } else { 
      $matches[$key] = rawurldecode($value); 
     } 
    } 

    // at this point there's a mismatch between the length of the rawurlencoded path 
    // that all other route helpers use, so we need to match their expectations 
    // to do that we build the matched part from the spec, using the matched params 
    $url = $this->spec; 
    $mergedParams = array_merge($this->defaults, $matches); 
    foreach ($mergedParams as $key => $value) { 
     $spec = '%' . $key . '%'; 
     if (strpos($url, $spec) !== false) { 
      $url = str_replace($spec, rawurlencode($value), $url); 
     } 
    } 
    // make sure the url we built from spec exists in the original uri path 
    if (false === strpos($uri->getPath(), $url)) { 
     return null; 
    } 
    // now we can get the matchedLength 
    $matchedLength = strlen($url); 

    return new RouteMatch($mergedParams, $matchedLength); 
} 
+0

Je viens de l'essayer sans 'UnicodeSegment' - et ça marche toujours ... Toutes les routes utilisées pour la requête ne doivent-elles pas être" unicode safe "? – automatix

+1

uniquement si vous appliquez des contraintes Unicode aux paramètres dans le segment lui-même, sinon ce n'est pas nécessaire – Crisp

+0

Quelle version de ZF2 est cette modification? [J'ai rencontré à peu près le même problème] (http://stackoverflow.com/q/29097676/313192) mais ce code ne fonctionne pas pour moi et je n'ai pas été capable de comprendre quels sont les bits à porter sur travailler contre 2.3.5. – Caleb

Questions connexes