Codeigniter 中的表单操作 3 6

Form action in Codeigniter 3 6

我正在使用 Codeigniter3.0.6,我继续正确设置表单操作,当我提交表单时它直接进入本地主机主页,我的代码如下
我设置了 $config['base_url'] = 'http://localhost:8081/consultant'; 因为没有它基础 url return 就像 http://::1/consultant 根据 this question

我的观点

<form role="form" action="<?=base_url();?>login/auth" method="post" class="login-form" id="login-form">
        <div class="form-group">
            <label class="sr-only" for="form-username">Username</label>
            <input type="text" name="form-username" placeholder="Username..." class="formusername form-control" id="form-username">
       </div>
       <div class="form-group">
            <label class="sr-only" for="form-password">Password</label>
            <input type="password" name="form-password" placeholder="Password..." class="form-password form-control" id="form-password">
       </div>
       <button type="submit" class="btn">Sign in!</button>
    </form> 

我在检查元素时使用了 base_url('login/auth')site_url('login/auth');,它显示正确的 url 就像 http://localhost:8081/consultant/login/auth ,但是应用程序转到本地主机主页。

您有 htaccess 文件吗? 如果没有,您需要使用

<form action="<?= base_url('index.php/login/auth')?>" method="post">

如果是,你是对的

<form action="<?= base_url('login/auth')?>" mehtod="post">

以htaccess文件为例

<IfModule mod_rewrite.c>
    RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /consultant/index.php?/ [L]
</IfModule> 

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /consultant/index.php
</IfModule>

我终于找到了答案。在 Codeigniter 2 中,htaccess 文件 RewriteRule 就像

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

但在 Codeigniter 3 中,您需要将项目名称放在 index.php 之前。例如我的是:"consultant"

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

请记住,您将 base_url 设置为如下所示:

$config['base_url'] = 'protocol://localhost/projectname';

我的如下:

$config['base_url'] = 'http://localhost:8081/consultant';

它工作正常。