zf2 root url静态和动态

时间:2015-09-29 14:17:20

标签: frameworks zend-framework2 url-routing

我目前正在使用静态路由,但我想知道你是否可以同时拥有静态和动态路由。所以我正在使用它:

    'contact' => array(
        'type' => 'literal',
        'options' => array(
            'route'    => '/contact',
            'defaults' => array(
                'controller' => 'Application\Controller\IndexController',
                'action'     => 'contact',
            ),
        ),
    ),

我想和

一起使用这样的东西
    'contact' => array(
        'type' => 'segment',
        'options' => array(
            'route'    => '/:curated_url',
            'defaults' => array(
                'controller' => 'Application\Controller\IndexController',
                'action'     => 'curated',
            ),
        ),
    ),

动态路由会接受所有内容并劫持静态路由。那么我正在考虑使用动作路由,但curated_url可以是任何东西。

有没有办法可能使用动态的默认设置,如果它不在那里,那么它会在数据库中查看它是否在那里?

**********编辑**********

所以这就是我暂时保留的东西,它有效,但我知道有更好的方法,我只是不知道更好的方法。

router.config.php

'curated-url' => array(
    'type' => 'Zend\Mvc\Router\Http\Segment',
    'options' => array(
        'route'    => '/:url',
        'defaults' => array(
            'controller' => 'Application\Controller\IndexController',
            'action'     => 'custom',
        ),
    ),
),

IndexController.php

/*
 * Special kind of special
 */
public function customAction()
{
    $params = array();
    $type = $this->params('url');

    if ('contact' == $type) {
        $params = $this->contactAction($this->getRequest());
    } else if ('discover' == $type) {
        $params = $this->discoverAction();
    } else if ('how-it-works' == $type) {
        $params = $this->howItWorksAction();
    } else if ('privacy-policy' == $type) {
        $params = $this->privacyPolicyAction();
    } else if ('questions' == $type) {
        $params = $this->questionsAction();
    } else if ('terms-of-use' == $type) {
        $params = $this->termsOfUseAction();
    } else if ('volunteer' == $type) {
        $params = $this->volunteerAction();
    } else {
        $params = $this->curatedAction($type);
        $type = 'curated';
    }

    $view = new ViewModel($params);
    $view->setTemplate('application/index/' . $type);

    return $view;
}

1 个答案:

答案 0 :(得分:0)

我不太确定这是不是你的意思,但我相信你需要指定约束键,让我告诉你如何:

'contact' => array(
    'type' => 'segment',
    'options' => array(
        'route'    => '/:curated_url',
        'constraints' => array(
            'curated_url' => '[a-zA-Z][a-zA-Z0-9_-]+',
        ),
        'defaults' => array(
            'controller' => 'Application\Controller\IndexController',
            'action'     => 'curated',
        ),
    ),
),

那么如果你传递的东西如下:

https://yoururl.com/aaaAAA1_2

它将指向策划的行动。