问题描述
我有一个网站,需要允许多个URL结构。例如:
www.examplesite.com/people/add //<-示例公司
www。 otherexample.com/xyz/people/add //<- xyz公司(不基于位置)
www.otherexample.com/florida/abc/people/add //<- abc公司(基于位置)
每个URL都应能够基于该URL检测到哪个公司。
到目前为止,我已经能够解析URL来确定它是哪家公司,但是如何添加这些额外的 / florida / abc /
路由的一部分,以允许应用程序的其余部分正常工作?
我已经尝试了很多方法,包括在路由文件顶部将变量设置为 / florida / abc(或任何变量),然后在每个路由之前添加该变量,但这不能处理每个控制器/动作,而且看起来很命中或错过/拥挤。
我也使用 admin
前缀,因此例如,它也需要像这样工作: / p>
www.otherexample.com/admin/florida/abc/people/add
我的假设是我需要使用 routes.php
文件,但是我无法确定该如何实现。
为每种情况创建一条路线似乎是要走的路:
Router :: connect('/ people / add',array('controller'=> 人民,行动 => '加'));
Router :: connect('/:company / people / add',array('controller'=>'people','action'=>'add'),array('pass'=> array('company'),'company'=>'[az] +'));
Router :: connect('/:location /:company / people / add',array('controller'=>'people','action'=>'add'),array('pass' => array('company','location'),'company'=>'[az] +','location'=>'[az] +')));
然后控制器可以接收以下值:
公共函数add($ company ='',$ location =''){
var_dump($ company,$ location);出口;
}
在路由中注意正则表达式并进行修改以匹配您的传入数据。
I have a site that needs to allow multiple URL structures. For example:
www.examplesite.com/people/add // <-- example company
www.otherexample.com/xyz/people/add // <-- "xyz" company (not location based)
www.otherexample.com/florida/abc/people/add //<-- "abc" company (location based)
Each URL should be able to detect which company it is based on the URL.
So far, I've been able to parse out the URL just fine to determine which company it is, but how to I add these extra /florida/abc/
parts to the routes to allow the rest of the app to work?
I've tried a number of things including setting a variable to the '/florida/abc' (or whatever it is) at the top of the routes file, then adding that before each route, but that doesn't handle every controller/action and seems very hit or miss/buggy.
I also use the admin
prefix, so for example, it would also need to work like this:
www.otherexample.com/admin/florida/abc/people/add
My assumption is that I need to use the routes.php
file, but I can't determine within that how I can make this happen.
Creating a route for each case seems the way to go:
Router::connect('/people/add', array('controller' => 'people', 'action' => 'add'));
Router::connect('/:company/people/add', array('controller' => 'people', 'action' => 'add'), array('pass' => array('company'), 'company' => '[a-z]+'));
Router::connect('/:location/:company/people/add', array('controller' => 'people', 'action' => 'add'), array('pass' => array('company', 'location'), 'company' => '[a-z]+', 'location' => '[a-z]+'));
Then the controller can receive these values:
public function add($company = '', $location = '') {
var_dump($company, $location); exit;
}
Mind the regex in routes and amend to match your incoming data.
这篇关于CakePHP路由提供不同级别的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!