简体   繁体   English

路由问题-Codeigniter

[英]Routing Issue - Codeigniter

I have pages that are generated from a database, based on the URI. 我有根据URI从数据库生成的页面。 Functons as it should, however I can't set-up my routes to eliminate the controller and function from the URL. 可以使用的功能,但是我无法设置routes来消除URL中的控制器和功能。

$route['studios/(:any)'] = 'studios/location/$1';

Right now, I have the route to show the controller name and the URI variable (whatever that may be). 现在,我有了显示控制器名称和URI变量(无论可能是什么)的路由。 However, I want to eliminate the controller name as well and just display the URI variable that's called as the URL. 但是,我也想删除控制器名称,只显示被称为URL的URI变量。 Hard to explain - hopefully someone picks up my drift... 很难解释-希望有人接我的漂移...

Current URL would be: domain.com/studios/studio1 当前网址为: domain.com/studios/studio1

But I want to just display: domain.com/studio1 但我只想显示: domain.com/studio1

I tried $route['/(:any)'] = 'studios/location/$1'; 我试过$route['/(:any)'] = 'studios/location/$1'; , but that's messing up my entire site. ,但那弄乱了我的整个网站。

Help? 救命?

$route['studios(/:any)*'] = 'studios/location';

This route will force everything from studios on to studios/location . 这条路线将迫使从studiosstudios/location所有事物。 You can then access any of the parameters using URI segments: 然后,您可以使用URI段访问任何参数:

$id = $this->uri->segment(2);

If your URL was somewhere.com/studios/location/2 , $id would resolve to 2 如果您的网址是somewhere.com/studios/location/2 ,则$id将解析为2

However, since you want it to just be from the root on, you will have to put your override route at the bottom of the routes file so it is assessed last: 但是,由于您希望它仅是根目录,因此您必须将替代路由放置在路由文件的底部,以便最后评估它:

// all other routes here. Which must be specifically 
// defined if you want a catch all like the one you mentioned
$route['(:any)'] = 'studios/location';

Alternatively, if you want a high maintenance site, you can specify a collection of routes like so: 另外,如果您想要一个高维护性的站点,则可以指定如下路由集合:

$route['(studio1|studio2|studio3)'] = 'studios/location/$1';

how is it "messing up your site"? 它如何“弄乱您的网站”?

In any case, you should not have the / before (:any) 无论如何,您都不应该在/之前(:any)

Just: 只是:

$route['(:any)'] = 'studios/location/$1';

EDIT: 编辑:

BEFORE the $route['(:any)'], you'll need to specify routes fro all your controllers; 在$ route ['(:any)']之前,您需要指定所有控制器的路由; this is pretty normal, don't know if I'd call it "high maintenance", but you'll need to decide 这很正常,不知道我是否称其为“高维护”,但是您需要确定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM