加载控制器没有$ route [英] Load controller without $route

查看:269
本文介绍了加载控制器没有$ route的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

exmple:此载入默认控制器/类的函数

exmple: this load default controller/class with function page,

www.example.com/page

,除非我们有controller / class page AND set $ route ['page'] ='page'; 但是如果我们不设置 $ route ,它仍然会加载default_controller。

unless we have controller/class named page AND set $route['page'] = 'page'; it'll load the controller. But if we dont set the $route, it'll still load default_controller.

是真的a 控制器必须有 $ route [''] 总是?是不可能加载控制器没有设置 $ route [''] 具有相同名称的控制器功能?

is that true a controller must have a $route[''] always? is not it possible to load controller page without set $route[''] even there is no default controller function with same name?

编辑

访问

www.mysite.com/index.php/user

我有用户控制器,但我的路线文件只包含:

I do have user controller with index function, but my route file only contain:

$route['default_controller'] = 'page';
$route['(:any)'] = 'page/$1';
$route['product'] = 'product';
//$route['user'] = 'user';
$route['404_override'] = '';

返回 404 this: $ route ['user'] ='user';

为什么?

谢谢。

推荐答案

不,这不是真的。 CodeIgniter,默认情况下,直接将URI段映射到:

No, that's not true. CodeIgniter, by default, directly maps URI segments to:

example.com/index.php/controller/method/param/param/...

或者如果您有一个.htaccess /类似的解决方案来删除index.php:

Or if you have an .htaccess / similar solution to remove index.php:

example.com/controller/method/param/param/...

路由用于您希望使用不会

编辑:您有冲突的路线。 CodeIgniter将从顶部到底部按顺序查看每个路由,如果找到匹配的路由,它将停止查找和处理该路由。因为您有(:any)全部接收路线,所以它会匹配任何(就像它说的)。

Edit: You have conflicting routes. CodeIgniter will look at each route in order from top to bottom, and if it find one that matches, it stops looking and processes that route. Because you have an (:any) catch-all route, it will match anything (like it says).

经验法则是首先放置你最具体的路由,然后得到更多的泛型和后面的catch。您的(:any)路由应该是您的列表中最后一个。而且默认控制器和404覆盖应该保持优先。

The rule of thumb is to place your most specific routes first, and then get more generic and catch-all later. Your (:any) route should be the very last one in your list. And the default controller and 404 overrides should stay first.

$route['default_controller'] = 'page';
$route['404_override'] = '';

$route['product'] = 'product';
$route['user'] = 'user';
$route['(:any)'] = 'page/$1';

您需要添加产品 code>((:any)路由)code> user 如果你想避免为每一个现有控制器编写路由规则,但仍然利用全部控制器,请考虑使用 404_override 控制器/方法。您可以进行验证,以检查URI是否有效。只要确保不会抛出404错误(您可以使用 show_404()),因为任何不存在的URL将被路由到那里。

You need to add the product and user routes because you've defined the (:any) route. If you want to avoid writing route rules for every one of your existing controllers, but still take advantage of a catch-all controller, consider using the 404_override controller/method instead. You can do your verifications to check if the URI is valid there. Just make sure to throw a 404 error if not (you can use show_404()), since any non-existant URL will be routed to there.

这篇关于加载控制器没有$ route的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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