0

I have a ZF2 app with a few modules and their routes. Everything works great. But I'd like to customize the way URL are created, without having to rewrite all my views.

let consider the following route:

'routes' => array(
    'moduleName' => array(
        'type'    => 'segment',
        'options' => array(
            'route'    => '/moduleName/[:title]-[:id][/:action]
            'constraints' => array(
                'action'    => '[a-zA-Z][a-zA-Z0-9_-]*',
                'id'        => '[0-9]+',
            ),
            'defaults' => array(
                'controller' => 'moduleName\Controller\moduleName',
                'action'     => 'index',
            ),
        ),
    ),

I've put no constraints on 'title' because it can be a string containing european special char and/or spaces. the url http://domain/moduleName/J'ustAn éxample-28/create works fine but I'd like it to be proper formatted: http://domain/moduleName/j-ustan-example-28/create

is there a way to this nicely in my config file? of Module.php? or even 'higher'?

EDIT I tried to build a new Url Helper (based on this question Extending Zend\View\Helper\Url in Zend Framework 2):

namespace Application\View\Helper;

use Zend\View\Helper\Url as ZendUrl;

class Url extends ZendUrl {

    public function __invoke($name = null, array $params = array(), $options = array(), $reuseMatchedParams = false) {
        foreach($params as $param => $value) {
            $params[$param] = $this->cleanString($value);
        }
        $link = parent::__invoke($name, $params, $options, $reuseMatchedParams);
        return $link;
    }

    public function cleanString($string) {
        $string = str_replace(array('[\', \']'), '', $string);
        $string = preg_replace('/\[.*\]/U', '', $string);
        $string = preg_replace('/&(amp;)?#?[a-z0-9]+;/i', '-', $string);
        $string = htmlentities($string, ENT_COMPAT, 'utf-8');
        $string = preg_replace('/&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);/i', '\\1', $string );
        $string = preg_replace(array('/[^a-z0-9]/i', '/[-]+/') , '-', $string);
        return strtolower(trim($string, '-'));
    }
}

But I cannot make it work. I've put this in \Application\Module.php:

public function getViewHelperConfig()
{
    return array(
        'factories' => array(
            'Application\View\Helper\Url'            => function ($sm) {
                $serviceLocator = $sm->getServiceLocator();
                $view_helper =  new \Application\View\Helper\Url();
                $router = \Zend\Console\Console::isConsole() ? 'HttpRouter' : 'Router';
                $view_helper->setRouter($serviceLocator->get($router));

                $match = $serviceLocator->get('application')
                    ->getMvcEvent()
                    ->getRouteMatch();

                if ($match instanceof RouteMatch) {
                    $view_helper->setRouteMatch($match);
                }

                return $view_helper;
            }
        ),
    );
}

But it does not work and does not display any errors.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Bibear
  • 69
  • 9
  • 1
    DO you want to accept the url with special characters and just modify the parameter to be filtered, or redirect the special character url to the transformed url? – GeeH Oct 19 '13 at 09:40
  • I want to filter the params.I tried to build a new URL Helper but cannot manage to make it work ... – Bibear Oct 21 '13 at 07:41

1 Answers1

0

Maybe like this

'title'    => '[a-zA-Z][a-zA-Z0-9_-]*', // add this line to your route

Otherwise when you create the link

function clean($string) {
   $string = str_replace('', '-', $string); // Replaces all spaces with hyphens.
   return preg_replace('/[^A-Za-z0-9\-]/', '-', $string); // Removes special chars.
}
$title = clean("J'ustAn éxample");

Generate link

echo $this->url('moduleName', array('title'=>$title,'id'=>28,'action'=>'create'))

source: Remove all special characters from a string

Community
  • 1
  • 1
Remi Thomas
  • 1,528
  • 1
  • 15
  • 25