ZF2修改URL格式

问题描述 投票:0回答:1

我有一个ZF2应用,其中包含一些模块及其路由。一切正常。但我想自定义URL的创建方式,而不必重写我的所有视图。

让我们考虑以下路线:

'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',
            ),
        ),
    ),

我对'title'没有任何限制,因为它可以是包含欧​​洲特殊字符和/或空格的字符串。网址http://domain/moduleName/J'ustAn éxample-28/create可以正常工作,但我希望其格式正确:http://domain/moduleName/j-ustan-example-28/create

在我的配置文件中有没有办法做到这一点? Module.php?甚至“更高”?

编辑我试图构建一个新的网址助手(基于此问题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, '-'));
    }
}

但是我无法使其正常工作。我把它放在\ 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;
            }
        ),
    );
}

但是它不起作用,并且不显示任何错误。

php routes zend-framework2 url-routing
1个回答
0
投票

也许像这样

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

否则,当您创建链接时

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");

生成链接

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

来源:Remove all special characters from a string

© www.soinside.com 2019 - 2024. All rights reserved.