【问题标题】:CodeIgniter routes problem with controller's index functionCodeIgniter 使用控制器的索引函数路由问题
【发布时间】:2011-07-05 05:02:42
【问题描述】:

我目前的开发正在使用 CodeIgniter,当我尝试将一些参数传递给我的主控制器的索引函数时,它返回给我404 error

http://localhost/gis/1 -> 404 ERROR
http://localhost/gis/home/1 -> 404 ERROR

其实我的route.php 文件有这样的路径:

$route['default_controller'] = "home";    

$route['home/([:num])'] = 'home/index/$1';

我的.htaccess 文件是这个(来自this post 从CI urls 中删除index.php):

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>

还有我的config.php 文件:

$config['base_url'] = "http://localhost/gis";

$config['index_page'] = "";

我做错了什么?谢谢!

【问题讨论】:

  • routes.php 中的默认控制器是什么?
  • @Sarfraz 我编辑我自己的问题

标签: .htaccess codeigniter routing codeigniter-url


【解决方案1】:

更新:

引用 OP:

.htaccess 没有罪。问题 是我的路线需要 $route['home/(:num)'] = ... 我添加了 括号和所有作品。


而不是:

$route['home/([:num])'] = 'home/index/$1';

尝试像这样指定它:

$route['home/:num'] = 'home/index/$1';

还要确保从application/config/routes.php 开始,您的默认控制器设置为home

$route['default_controller'] = 'home';

【讨论】:

  • 感谢您的快速回答。我试过你说的,但对我没有用。如果我尝试在主控制器索引函数中打印参数,请始终打印“$1”。你能帮帮我吗?
  • @Fran Verona:尝试禁用 .htaccess 文件以检查其中是否有问题。
  • htaccess 无罪。问题是我的路线需要是 $route['home/(:num)'] = ... 我添加了括号和所有作品。您可以编辑自己的答案以添加此内容吗?非常感谢 Sarfraz。
  • @Fran:很高兴知道它有效,我认为您已经在问题中使用了括号。不过我会更新答案:)
猜你喜欢
  • 2011-05-09
  • 1970-01-01
  • 2015-10-04
  • 1970-01-01
  • 2015-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多