Codeigniter中的路由 - 自动 [英] Routes in Codeigniter - Automatically

查看:262
本文介绍了Codeigniter中的路由 - 自动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Codeigniter路由的问题。我想在我的网站上的所有注册用户都有自己的目录,例如: www.example.com/username1 www.example .com / username2 。这个目录应该映射到控制器polica,方法ogled,参数username1。



如果我这样做,这个路由:polica / ogled / parameter。不正确:

  $ route [(:any)] =polica / ogled / $ 1 

这可以工作,但我一直手动输入信息 routes.php

  $ route [username1] =polica / ogled / username1 

如何做到这一点将被自动化?



UPDATE:
例如,我有一个名为 ads 的控制器。例如,如果您转到 www.example.com/ads /
,则会显示列出的广告。如果您转到 www.example.com/username1 ,则会列出用户 username1 的广告。还有控制器用户配置文件 latest ,。 ..



我当前 routes.php

  $ route ['oglasi'] ='oglasi'; 
$ route ['(:any)'] =polica / ogled / $ 1
$ route ['default_controller'] ='domov'
$ route ['404_override'] ='';

我解决了此代码的问题:

  $ route ['oglasi /(:any)'] ='oglasi / $ 1'; 
$ route ['(:any)'] =polica / ogled / $ 1
$ route ['default_controller'] ='domov'
$ route ['404_override'] ='';

问题,Mario

解决方案

你的路线的问题是,通过使用:任何你匹配,实际上...任何路线,所以你几乎卡在那里。
我想你可能有两个解决方案:



1)你可以选择性地重新路由所有的站点控制器,如:

  $ route ['aboutus'] =aboutus; 
$ route ['where-we-are'] =whereweare;
//为所有网站的控制器执行此操作
//最后:
$ route ['(:any)'] =polica / ogled / $ 1

所有这些路由都必须在ANY之前,因为它们按提交的顺序读取,



在注释后编辑

b

我的意思是,如果你要与任何段匹配,这意味着你不能使用任何控制器(默认情况下,第一个URI段),因为路由器将始终使用您定义的法律重新路由。
为了允许CI执行其他控制器(无论是什么,我只是使用一些常见的网页,但可以是字面上的一切),你需要允许他们从重新路由中排除它们。您可以通过将它们放在您的ANY规则之前实现这一点,以便每当CI通过您的路由规则,它首先解析您转义的,并且只有如果它们不匹配在URL上找到的任何东西,它传递到任何规则。



我知道这是一个代码冗长,但它们肯定会小于6K,你说。
因为我不知道你的URL和你的web应用程序的实际结构,这是我想到的唯一的解决方案。如果您提供更多信息,例如如何整理应用程序的常规网址,那么我可以更新我的回答



/结束编辑 / p>

这不是一个普通的解决方案,因为它需要很多代码,但是如果你想要一个这样的设计,这是我的想法的唯一方式。
另外,考虑你可以使用regexes作为$ route索引,但我不认为它可以在这里工作,因为你的用户名不太可能匹配在这种方式,但我只是想指出的可能性。 p>



2)您可以稍微更改设计模式,

  $ route ['user /(:any)'] =polica / ogled / $ 1; 

这将生成非常漂亮的(和语义)URL,并且将避免逃脱所有的麻烦其他路线。


I have a problem with Codeigniter routes. I would like to all registered users on my site gets its own "directory", for example: www.example.com/username1, www.example.com/username2. This "directory" should map to the controller "polica", method "ogled", parameter "username1".

If I do like this, then each controller is mapped to this route: "polica/ogled/parameter". It's not OK:

$route["(:any)"] = "polica/ogled/$1";

This works, but I have always manually entered info in routes.php:

$route["username1"] = "polica/ogled/username1";

How do I do so that this will be automated?

UPDATE: For example, I have controller with name ads. For example, if you go to www.example.com/ads/ there will be listed ads. If you are go to www.example.com/username1 there are listed ads by user username1. There is also controller user, profile, latest,...

My Current routes.php:

$route['oglasi'] = 'oglasi';
$route['(:any)'] = "polica/ogled/$1"
$route['default_controller'] = 'domov';
$route['404_override'] = '';

I solved problem with this code:

$route['oglasi/(:any)'] = 'oglasi/$1';
$route['(:any)'] = "polica/ogled/$1"
$route['default_controller'] = 'domov';
$route['404_override'] = '';

Regards, Mario

解决方案

The problem with your route is that by using :any you match, actually...ANY route, so you're pretty much stuck there. I think you might have two solutions:

1)You can selectively re-route all your sites controller individually, like:

$route['aboutus'] = "aboutus";
$route['where-we-are'] = "whereweare";
//And do this for all your site's controllers
//Finally:
$route['(:any)'] = "polica/ogled/$1";

All these routes must come BEFORE the ANY, since they are read in the order they are presented, and if you place the :any at the beginning it will happily skip all the rest.

EDIT after comment:

What I mean is, if you're going to match against ANY segment, this means that you cannot use any controller at all (which is, by default, the first URI segment), since the router will always re-route you using your defined law. In order to allow CI to execute other controllers (whatever they are, I just used some common web pages, but can be literally everything), you need to allow them by excluding them from the re-routing. And you can achieve this by placing them before your ANY rule, so that everytime CI passed through your routing rules it parses first the one you "escaped", and ONLY if they don't match anything it found on the URL, it passes on to the :ANY rule.

I know that this is a code verbosity nonetheless, but they'll surely be less than 6K as you said. Since I don't know the actual structure of your URLs and of your web application, it's the only solution that comes to my mind. If you provide further information, such as how are shaped the regular urls of your app, then I can update my answer

/end edit

This is not much a pratical solution, because it will require a lot of code, but if you want a design like that it's the only way that comes to my mind. Also, consider you can use regexes as the $route index, but I don't think it can work here, as your usernames are unlikely matchable in this fashion, but I just wanted to point out the possibility.

OR

2) You can change your design pattern slightly, and assign another route to usernames, something along the line of

$route['user/(:any)'] = "polica/ogled/$1";

This will generate quite pretty (and semantic) URLs nonetheless, and will avoid all the hassle of escaping the other routes.

这篇关于Codeigniter中的路由 - 自动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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