doufengsui7449 2014-04-18 15:06
浏览 78
已采纳

具有单个控制器错误404的Codeigniter

I'm quite a rookie with CodeIgniter, and as per title, I have troubles trying to setup a single controller for my application. It's a very simple static site with couple of pages like "home", "about" and so on...

I have this in my routes.php file:

$route['default_controller'] = "mycontroller";
$route['404_override'] = '';
$route['(:any)'] = "mycontroller/$1";

And this in mycontroller.php file:

// Home
public function index()
{
    $data['page'] = 'home';
    $this->load->view('template',$data);
}

// about
public function about()
{
    $data['page']  = 'about';
    $data['title'] = 'About Us';
    $this->load->view('template',$data);
}

I'm working in a localhost environment, and the CI project is in this folder:

http://localhost/local/project/ci-tbs/

and I've specified it also in the config.php file for the base_url parameter.

Now what I'd expect pointing the browser to

http://localhost/local/project/ci-tbs/about

is to find the "About Us" page, instead I got a 404 error. Pointing to the base address corectly gives me the "Home" page.

What am I doing wrong?

Is it sensed to use a single controller istead of 1 per page? I'd totally do that in a quick way to fix, still I'm quite baffled by the fact that I can't understand what I am doing wrong and why it's not working. I'd like to simple set everything in one controller, one method per page.

I've already seen this topic asked here in SO, like using regular expressions in the route $route['(.*)'] = "mycontroller/$1";, but nothing really worked for my case wich I think is quite basic (so basic I'm sure my error is so gross that it will be quite embarrassing :P ).

Additional info:

I have in the folder an .htaccess file picked as is from the Html5 Boilerplate, tried with and without it but 404 is always there. I'm using XAMPP as local environment.


For answer

As mentioned by @Vincent Decaux in the answer, the deal to fix this was to add index.php in the url, the other interesting part is

Create your .htaccess file to "hide" index.php

This way I've resolved another small issue for the pages with missing findings for the assets files, so I used the following rule in the .htaccess file, redirecting all requests to the index.php file and excluding files in assets folder and images, along with robots.txt as suggested here https://stackoverflow.com/a/11846150/1262357

RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

hope this helps others with same problems I had!

  • 写回答

1条回答 默认 最新

  • dongtu7205 2014-04-18 17:08
    关注

    As mentionned in my comment, it seems to work using :

    localhost/local/project/ci-tbs/index.php/about 
    

    Create your .htaccess file to "hide" index.php.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向