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

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

问题描述

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

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".

如果我喜欢这个,那么每个控制器都会映射到这个路由:polica/ogled/parameter".这不正常:

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";

这有效,但我总是在 routes.php 中手动输入信息:

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

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

我该如何实现自动化?

更新:例如,我有一个名为 ads 的控制器.例如,如果您访问 www.example.com/ads/会有列出的广告.如果您访问 www.example.com/username1,则会看到用户 username1 列出的广告.还有控制器userprofilelatest、...

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,...

我当前的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'] = '';

问候,马里奥

推荐答案

你的路线的问题在于,通过使用 :any 你匹配,实际上......任何路线,所以你几乎卡在那里.我认为您可能有两种解决方案:

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)您可以有选择地单独重新路由所有站点控制器,例如:

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";

所有这些路由必须在 ANY 之前出现,因为它们是按照它们出现的顺序阅读的,如果你把 :any 放在开头,它会很高兴地跳过所有其余的.

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.

评论后

我的意思是,如果您要匹配任何段,这意味着您根本不能使用任何控制器(默认情况下,这是第一个 URI 段),因为路由器将始终重新路由你使用你定义的法律.为了允许 CI 执行其他控制器(不管它们是什么,我只是使用了一些常见的网页,但实际上可以是所有的),您需要通过将它们从重新路由中排除来允许它们.你可以通过将它们放在你的任何规则之前来实现这一点,这样每次 CI 通过你的路由规则时,它首先解析你转义"的那个,并且只有当它们不匹配它在 URL 上找到的任何内容时,它才会传递到 :ANY 规则.

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.

尽管如此,我知道这是一个冗长的代码,但正如您所说,它们肯定会少于 6K.由于我不知道您的 URL 和 Web 应用程序的实际结构,因此这是我想到的唯一解决方案.如果您提供更多信息,例如您的应用的常规 url 是如何形成的,那么我可以更新我的答案

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

/结束编辑

这不是一个实用的解决方案,因为它需要大量代码,但是如果您想要这样的设计,这是我想到的唯一方法.另外,考虑您可以使用正则表达式作为 $route 索引,但我认为它不能在这里工作,因为您的用户名不太可能以这种方式匹配,但我只是想指出这种可能性.

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.

2) 你可以稍微改变你的设计模式,并为用户名分配另一条路线,类似于

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";

尽管如此,这将生成非常漂亮(和语义)的 URL,并且将避免转义其他路由的所有麻烦.

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

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

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