【问题标题】:CakePHP Router::connect() config, custom parameter parsingCakePHP Router::connect() 配置,自定义参数解析
【发布时间】:2010-11-02 21:50:28
【问题描述】:

我有以下 routes.php 配置

Router::connect('/:type/:slug;:id', array(
        'controller' => 'content',
        'action' => 'show',
        'type' => null,
        'slug' => null,
        'id' => null,
    ),
    array(
        'type' => '(articles|releases|answers|videos)',
        'slug' => '[a-zA-Z0-9\-]+',
        'id' => '[0-9]+',
        'pass' => array('type', 'slug', 'id'),
    ));

我正在尝试解析以下 URL:

/answers/effective-language-therapy-for-people;368

路由器让我找到正确的控制器和操作,但转储$this->params 表明它没有正确识别$id$slug

Array
(
    [type] => answers
    [slug] => answers
    [id] => effective-language-therapy-for-people
    [named] => Array
        (
        )

    [pass] => Array
        (
            [0] => answers
            [1] => answers
            [2] => effective-language-therapy-for-people
        )

    [controller] => content
    [action] => show
    [plugin] => 
    [url] => Array
        (
            [ext] => html
            [url] => answers/effective-language-therapy-for-people;368
        )

    [form] => Array
        (
        )
)

那么 - 什么给了?我的正则表达式是错误的,方法是遗漏了什么,还是什么?有什么想法吗?

注意:我已阅读:


更新、解决和工作版本

Router::connect('/:type/:slug:splitter:id', array('controller' => 'content', 'action' => 'view',), array(
    'type' => 'articles|releases|answers|videos',
    'slug' => '[a-zA-Z0-9\-]+',
    'splitter' => '[\;\-\|]+',
    'id' => '[0-9]+',
    ));

【问题讨论】:

  • 我不喜欢 slug 和数字之间的分号。
  • 明白了,我关心的可能是破折号或其他任何东西,但我正在减少此测试可能出现的问题。

标签: regex cakephp cakephp-1.3 router


【解决方案1】:

试试:

Router::connect('/:type/:slug;:id', array(
    'controller' => 'content',
    'action' => 'show',
    'type' => null,
    'slug' => null,
    'id' => null,
),
array(
    'type' => 'articles|releases|answers|videos',
    'slug' => '[a-zA-Z0-9\-]+',
    'id' => '[0-9]+',
    'pass' => array('type', 'slug', 'id'),
));

问题在于括号中的类型 (),CakePHP 不支持这种类型。

【讨论】:

  • 就是这样,把代码改成:Router::connect('/:type/:slug:splitter:id', array('controller' => 'content', 'action' = > 'view',), array( 'type' => 'articles|releases|answers|videos', 'slug' => '[a-zA-Z0-9\-]+', 'splitter' => ' [\;\-\|]+', 'id' => '[0-9]+', ));
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多