Codeigniter URI 路由不起作用 404 错误

Codeigniter URI routing not working 404 error

我的观点是:-

<li class="">
  <a class="" href="<?php echo base_url(); ?>Login"> 
  <i class="fa fa-unlock-alt"></i>&nbsp;Login </a>
</li>

我的登录控制器

class Login extends CI_Controller {
    public function __construct(){
        parent::__construct();
    }
    public function index()
    {
         $this->load->view('login');
    }
}

当我点击 link 时,它显示找不到 404 页面。 但是当手动编辑 link 并编写 /index.php/Login 时,它工作正常。如何解决这个问题...

尝试用 site_url() 代替 base_url()

<a class="" href="<?php echo site_url(); ?>Login">

首先你应该在config/routes中正确定义url。php:

$route['login'] = 'login/index';

然后您可以在具有 site_url() 函数的模板中使用此路由:

<li>
  <a href="<?php echo site_url('login'); ?>"> 
  <i class="fa fa-unlock-alt"></i>&nbsp;Login </a>
</li>

在 webroot 中创建一个文件并将其命名为 .htaccess 然后在其中键入此代码

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?/ [L,QSA]
</IfModule>

然后在您的 application/config/config.php 文件中找到这个

$config ['index_page'] = '';

您还需要使用 3 个步骤:

  1. 在项目根目录(在您的项目文件夹“/project”或域根目录)上创建一个 .htaccess 文件

.htaccess 文件中插入以下代码行。

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php/ [L]

  1. 找到 config/congig.php 文件并将此行更改为-

*在我的例子中添加基础 url:

$config['base_url'] = 'http://localhost/cirest.dev/';(分配你的项目path/URL)

$config['index_page'] = 'index.php';$config['index_page'] = '';

  1. 将此 HTML 添加到您的视图中:

<li>

<a href="<?php echo base_url('login'); ?>">

<i class="fa fa-unlock-alt"></i>&nbsp;Login </a>

</li>

我认为它会起作用。 :)

我从 $route['login'] = ['login/index']; 更改为 $route['login' ] = 'login/index'; 在我的例子中。