CakePhp路由与控制器通配符 [英] CakePhp Routes with Controller wildcard

查看:258
本文介绍了CakePhp路由与控制器通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将所有 domainname.com/affiliateXYZ 的访问重新路由到AffController重新路由操作。但是如何?

I'd like reroute all domainname.com/affiliateXYZ visits to an AffController reroute action. But how?

'affiliate'的路由元素子字符串将是一致的,子字符串XYZ会有所不同。

The route element substring of 'affiliate' will be consistent and substring 'XYZ' will vary.

我会有优先项目要求,允许 domainname.com/aff/reroute/XYX (或 domainname.com/aff/reroute/CigarKing )在控制器/操作/参数格式,但唉。不是这样的情况。

I would've preferred project requirements that had allowed domainname.com/aff/reroute/XYX (or domainname.com/aff/reroute/CigarKing) in the controller/action/parameter format, but alas.. tis not the case.

最初我尝试以下失败:

Router::connect('/affiliate*',     array('controller' => 'aff', 'action' => 'reroute'));
Router::connect('/affiliate**',     array('controller' => 'aff', 'action' => 'reroute'));
Router::connect('/:controller*',     array('controller' => 'aff', 'action' => 'reroute'), array('controller'=>'(affiliate)');

一旦在控制器重新路由操作中,我会将affiliateXYZ展开为XYZ然后继续我知道如何做的事情我的问题是路由到控制器与affiliateXYZ作为第一个路由元素,同时不会弄乱当前的功能。

Once in the controller reroute action I will explode the affiliateXYZ into just 'XYZ' and then proceed with things I know how to do. My issue is routing to the controller with affiliateXYZ as the first routing element and at the same time not messing up current functionality.

我使用 Router :: connect('/:affiliate',array('controller'=>'aff','action'=>'reroute' & array('affiliate'))); 获取到控制器的信息,直到我看到对预先存在的控制器的负面影响(我假设,因为我没有显式列出所有其他控制器,

I'd used Router::connect('/:affiliate', array('controller' => 'aff', 'action' => 'reroute'),array('pass'=>array('affiliate'))); to get to the info into the controller, until I saw the negative impact on preexisting controllers (I assume because I didn't explicitly list all other controllers, which I don't think I should have to do.).

如何路由w /前缀(更准确地说是'affiliate'的子字符串)?

How to route w/prefix ( more accurately a substring of 'affiliate' )?

我知道前缀是错误的术语。

I know prefix is the wrong terminology.

Thx,提前。

推荐答案

您正在寻找正则表达式匹配,借助此功能,您可以定义特定路由元素的匹配方式。

You are looking for regular expression matching, with the help of this functionality you can define how specific route elements are matched.

此选项可用于 Router :: connect()的第三个参数。

元素中定义=> regex 格式:

This option is available for the third parameter of Router::connect().
You define it in element => regex format:

Router::connect(
    '/:affilate',
    array(
        'controller' => 'aff',
        'action' => 'reroute'
    ),
    array(
        'pass' => array('affilate'),
        'affilate' => 'affilate[^/]+'
    )
);

这将匹配以 affilate ,后跟一个或多个不是 / 的字符。

This would match any URL that starts with affilate, followd by 1 or more characters that are not a /.

另请参阅 http://book.cakephp.org/2.0/en/development/routing.html#route -elements

这篇关于CakePhp路由与控制器通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆