Code Igniter 控制器 - 无法将 uri-> 段与函数 index() 一起使用?
我正在使用代码点火器,由于某种原因,网址 http://mysite.com/account/100< /a> 给我一个 404 错误,但 http://mysite.com/account 实际上有效。这是我的控制器 account.php 的样子。
class account extends Controller {
function account()
{
parent::Controller();
}
function index()
{
echo 'hello';
echo $this->uri->segment(2);
}
}
知道出了什么问题吗?
I'm working with code igniter and for some reason, the url http://mysite.com/account/100 gives me a 404 error but http://mysite.com/account actualy works. Here's what my controller account.php looks like.
class account extends Controller {
function account()
{
parent::Controller();
}
function index()
{
echo 'hello';
echo $this->uri->segment(2);
}
}
Any idea what's wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我刚刚测试了一个像您一样的简单帐户类,它尝试调用 100 作为帐户方法,而不是您在index.php之后的URL应该是account/index/100并且它工作正常。
例如,可以使用路由来解决这个问题。
就是您正在寻找的。您可以查看 URI 路由 用户指南
了解更多信息。
CodeIgniter 要求您的控制器名称大写,文件名小写,因此请尝试将您的控制器更改为。
和你的文件名 account.php
I just tested a simple account class like you have and it is trying to call 100 as a method of account, instead your URL after index.php should be account/index/100 and it works fine.
This can be solved using routing for example.
is what you are looking for. You can look over the URI Routing user guide
for more info.
CodeIgniter requires that your controller name be capitalized, and the filename lowercase so try changing your controller to.
and your filename account.php
添加这条路线就可以了
Add this route and you are good
这不起作用,因为当您请求 http://example.com/account 时,它看起来index () 方法。
但是,当您请求 http://example.com/account/100 时,正在寻找 100() 方法。哪个不存在。
您可以像这样调整代码。
您将像这样调用网址: http://example.com/account/id/100
或者你可以这样做
http://example.com/account/alternative/100
This is not working because when you request http://example.com/account, it looks index() method.
But when you request http://example.com/account/100, is looking for 100() method. Which is not present.
You may tweak the code like this.
you'll call url like this: http://example.com/account/id/100
or you can do like this
http://example.com/account/alternative/100