CodeIgniter路由不接受与路由表不同的异常

发布于 2025-02-07 19:21:08 字数 1054 浏览 2 评论 0原文

不知何故,我陷入了路由规则。我无法为超过路由规则的页面设置路由。例如,我在 doutes.php 中都有:

$route['(:any)/(:any)/(:any)'] = 'pages/view/$1/$2/$3';
$route['(:any)/(:any)'] = 'pages/view/$1/$2';

因此,页面与地址正确加载:

  • mypage.com/value1/value2/value2/value3/
  • mypage.com/value.com/value1/value2/

,如果用户有意进入另一个用户值,它会抛出一个base_url()异常,该异常甚至都没有由我的404例异常来处理,所以我得到:

An uncaught Exception was encountered
Type: Error
Message: Call to undefined function base_url()

当然,我可以创建另一个规则来捕获4个值,但是5th值将丢弃该错误;)

我要实现的是找到一条规则,它将与任何超过最后一个规则的树相匹配,例如:

$route['(:any)/(:any)/(:any)/(no matter the value and tree)'] = 'pages/view/$1/$2/$3';
$route['(:any)/(:any)/(:any)'] = 'pages/view/$1/$2/$3';
$route['(:any)/(:any)'] = 'pages/view/$1/$2';

即使用户类型:

  • mypage.com/value.com/value1/value2/value3/4 也不会有任何错误/5/6/7/8/9 // 10/11/11 12/13/14/15/blabla/等...

我确定有解决方法:任何人都无法正常工作,它只会抓到一棵树),但是我没有找到任何树,我知道解决方案可能很简单,但是我还没有掌握CI路由...

预先感谢您任何提示。

somehow I got stuck with my routing rules. I can't set a routing for a page that exceeds the routing rule. For example I have in my routes.php:

$route['(:any)/(:any)/(:any)'] = 'pages/view/$1/$2/$3';
$route['(:any)/(:any)'] = 'pages/view/$1/$2';

so the page loads properly with addresses:

  • mypage.com/value1/value2/value3/
  • mypage.com/value1/value2/

However if a user intentionally enters another value, it will throw a base_url() exception, which is not even handled by my 404 exceptions catch, so I get:

An uncaught Exception was encountered
Type: Error
Message: Call to undefined function base_url()

I can, of course, create another rule to catch 4th value but then, 5th value will throw that error ;)

What I want to achieve is to find a rule, that will match to ANY tree that exceed last rule, something like:

$route['(:any)/(:any)/(:any)/(no matter the value and tree)'] = 'pages/view/$1/$2/$3';
$route['(:any)/(:any)/(:any)'] = 'pages/view/$1/$2/$3';
$route['(:any)/(:any)'] = 'pages/view/$1/$2';

So I won't have any errors even if a user type:

  • mypage.com/value1/value2/value3/4/5/6/7/8/9//10/11/12/13/14/15/blabla/etc...

I'm sure there is a workaround for this (some regex there? (:any) or :any will not work, it will catch only one tree more), however I didn't found any and I'm aware that solution may be simple, but I didn't mastered CI routing yet...

Thank you in advance for any hints.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖哒 2025-02-14 19:21:08

您可以使用像这样的普通正则:

$route['(:any)/(:any)/(:any)'] = 'pages/view/$1/$2/$3';
$route['(:any)/(:any)'] = 'pages/view/$1/$2';
$route['(:any)/(:any)/(:any)/.*'] = 'pages/view/$1/$2/$3';

更重要的问题是为什么不加载404?如果您的自定义404页面使用base_url()函数,则自动加载URL助手将解决该错误。

You can use a normal regex like so :

$route['(:any)/(:any)/(:any)'] = 'pages/view/$1/$2/$3';
$route['(:any)/(:any)'] = 'pages/view/$1/$2';
$route['(:any)/(:any)/(:any)/.*'] = 'pages/view/$1/$2/$3';

The more important question is why doesn't that load a 404? If your custom 404 page is using the base_url() function, autoloading the url helper will fix that error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文