【问题标题】:codeigniter routes is not workingcodeigniter 路由不工作
【发布时间】:2015-07-31 08:54:42
【问题描述】:

我在使用 CodeIgniter 时遇到以下问题。我正在尝试设置一个菜单,并使用路由配置来加载正确的内容,但由于某种原因它不起作用。

我在下面设置了 CodeIgniter:

http://localhost/new/CodeIgniter/

我的配置文件如下所示:

$config['base_url'] = 'http://localhost/new/CodeIgniter/';
$config['index_page'] = '';

我的路线配置如下所示:

$route['default_controller'] = 'Home/home';
$route['404_override'] = '';
$route['home'] = 'Home/home';
$route['compare'] = 'Home/home';
$route['signin'] = 'Home/home';
$route['translate_uri_dashes'] = FALSE;

这是我的 Home 控制器:

class Home extends CI_Controller {

public function __construct()
{
    parent::__construct(); 
} 

public function index() 
{
    $this->home();
}

public function home()
{
    $this->load->model('home_model');

    $this->load->view('header_view', $data);
    $this->load->view('nav_view', $data);
    $this->load->view('home_view', $data);
    $this->load->view('footer_view', $data);
}

public function compare()
{
    $this->load->model('home_model');

    $this->load->view('header_view', $data);
    $this->load->view('nav_view', $data);
    $this->load->view('home_view', $data);
    $this->load->view('footer_view', $data);
}

public function signin()
{
    $this->load->model('home_model');

    $this->load->view('header_view', $data);
    $this->load->view('nav_view', $data);
    $this->load->view('home_view', $data);
    $this->load->view('footer_view', $data);
}   

public function about()
{
    $this->load->model('home_model');

    $this->load->view('header_view', $data);
    $this->load->view('nav_view', $data);
    $this->load->view('home_view', $data);
    $this->load->view('footer_view', $data);
}   
}

当我访问http://localhost/new/CodeIgniter/ 时,我可以看到主页,但以下都不起作用:

http://localhost/new/CodeIgniter/home
http://localhost/new/CodeIgniter/compare
http://localhost/new/CodeIgniter/signin
http://localhost/new/CodeIgniter/about

有人知道我在这里做错了什么吗?

错误如下:

Not Found

The requested URL /new/CodeIgniter/home was not found on this server.

这是我的 httaccess

<IfModule authz_core_module>
    Require all denied
</IfModule>
<IfModule !authz_core_module>
    Deny from all
</IfModule>
<IfModule ModRewite>
    RewriteCond $1 !^(index\.php|images|robots\.txt)
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

【问题讨论】:

  • 你遇到了什么错误
  • 你需要一个默认控制器
  • @MathieuLescaudron 如果我删除默认控制器,我会收到以下错误消息:“无法确定应该显示什么。路由文件中没有指定默认路由。”
  • 是的,对不起,试试http://localhost/new/CodeIgniter/Home/homehttp://localhost/new/CodeIgniter/Home/compare
  • ok 尝试使用这个 url localhost/new/CodeIgniter/index.php/homelocalhost/new/CodeIgniter/index.php/compare 如果它与 htacess 的工作问题

标签: php codeigniter routes


【解决方案1】:

我认为缺少 .htaccess 文件。使用此内容创建一个新文件并将其保存在 CI 根目录中。

RewriteEngine on
RewriteBase /new/CodeIgniter
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA] 

【讨论】:

  • 我添加了 RewriteBase 行。您在子文件夹中有 CI。 :)
  • 打开文件 /config/config.php 并编辑 $config['log_threshold'] = 2;并重新加载浏览器,然后检查 /application/logs/log-xxx.php 中的日志文件 - 你会得到类似 ERROR - 2015-07-31 10:20:08 --> 404 Page Not Found --> xxx
【解决方案2】:

确保您启用了 mod_rewrite(通过 phpinfo 检查是否启用)。

为了启用 mod_rewrite 检查以下

  1. Linux
  2. window

并添加这个 .htacess

where to add

RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

在您的根文件夹中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 2018-06-27
    相关资源
    最近更新 更多