ZF2修改URL格式

时间:2013-10-17 17:01:16

标签: php routes zend-framework2 url-routing

我有一个带有几个模块及其路线的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的?甚至'更高'?

EDIT 我尝试构建一个新的Url Helper(基于此问题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;
            }
        ),
    );
}

但它不起作用,也没有显示任何错误......

有什么想法吗?

1 个答案:

答案 0 :(得分: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