php Codeigniter 中的路由 - 自动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7618633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:03:00  来源:igfitidea点击:

Routes in Codeigniter - Automatically

phpcodeigniterroutesrewrite

提问by Puzo

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

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

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

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

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

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

这有效,但我总是手动输入信息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/username1there are listed ads by user username1. There is also controller user, profile, latest,...

更新:例如,我有一个名为 name 的控制器ads。例如,如果您去www.example.com/ads/那里将列出广告。如果你去www.example.com/username1那里有用户列出的广告username1。还有控制器user, profile, latest,...

My Current routes.php:

我的当前routes.php

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

I solved problem with this code:

我用这段代码解决了问题:

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

Regards, Mario

问候, 马里奥

回答by Damien Pirsy

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:

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

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

1)您可以有选择地单独重新路由所有站点控制器,例如:

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

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.

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

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.

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

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

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

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

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

OR

或者

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

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

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

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

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

回答by jiabo.zhang

view this: http://www.web-and-development.com/codeigniter-minimize-url-and-remove-index-php/which includes remove index.php/remove 1st url segment/remove 2st url sigment/routing automatically.it will very helpful for you.

查看:http: //www.web-and-development.com/codeigniter-minimize-url-and-remove-index-php/包括自动删除 index.php/remove 1st url segment/remove 2st url sigment/routing .它对你很有帮助。

回答by alex iancu

Another way would be declaring an array with your intenal controllers and redirect everything else to the user controller like this in your routes.php file from codeigniter:

另一种方法是使用您的内部控制器声明一个数组,并将其他所有内容重定向到用户控制器,就像在您的 route.php 文件中来自 codeigniter 的:

$controllers=array('admin', 'user', 'blog', 'api');
if(array_search($this->uri->segment(1), $controllers)){
    $route['.*'] = "polica/ogled/";
}

回答by Chamilyan

I was struggling with this same problem very recently. I created something that worked for me this way:

我最近也在为同样的问题苦苦挣扎。我以这种方式创建了对我有用的东西:

Define a "redirect" controller with a remapmethod. This will allow you to gather the requests sent to the contoller with any proceeding variable string into one function. So if a request is made to http://yoursite/jeff/or http://yoursite/jamieit won't hit those methods but instead hit http://yoursite/remap function. (even if those methods/names don't exist and even if you have an index function, it supersedes it). In the _Remap method you could define a conditional switch which then works with the rest of your code re-directing the user any way you want.

使用重映射方法定义“重定向”控制器。这将允许您将发送到控制器的请求与任何正在进行的变量字符串收集到一个函数中。因此,如果向其发出请求,http://yoursite/jeff/或者http://yoursite/jamie它不会命中这些方法,而是会命中重http://yoursite/映射功能。(即使这些方法/名称不存在,即使您有索引函数,它也会取代它)。在 _Remap 方法中,您可以定义一个条件开关,然后该开关与您的其余代码一起使用,以您想要的任何方式重定向用户。

You should then define this re-direct controller as the default one and set up your routes like so:

然后,您应该将此重定向控制器定义为默认控制器,并像这样设置路由:

$route['(.*)'] = "redirect/index/";
$route['default_controller'] = "redirect";

This is at first a bit of a problem because this will basically force everything to be re-directed to this controller no matter what and ultimately through this _remapswitch.

起初这有点问题,因为这基本上会强制一切都重新定向到这个控制器,无论如何,最终通过这个_remap开关。

But what you could do is define the rules/routes that you don't want to abide to this condition above those route statements.

但是您可以做的是在这些路由语句之上定义您不想遵守此条件的规则/路由。

i.e

IE

$route['myroute'] = "myroute";
$route['(.*)'] = "redirect/index/";
$route['default_controller'] = "redirect";

I found that this produces a nice system where I can have as many variable users as are defined where I'm able to redirect them easily based on what they stand for through one controller.

我发现这会产生一个很好的系统,在该系统中我可以拥有与定义的一样多的可变用户,我可以根据它们通过一个控制器的代表内容轻松地重定向它们。