【问题标题】:How to fix requested URL not found error in Codeigniter如何修复 Codeigniter 中未找到请求的 URL 错误
【发布时间】:2019-06-16 16:18:31
【问题描述】:

我是 codeigniter 的新手,我正在尝试使用它构建一个注册页面。当我在控制器中调用函数时,服务器返回“在此服务器上找不到请求的 URL /Users/actionRegister”错误。

我更改了 .htaccess 文件并启动了 apache 模块上的 rewrite_mode。

<?php echo form_open('Users/actionRegister'); ?>

    <div class="form-group">
        <input type="text" name="name" class="form-control" id="name" placeholder="Name">
    </div>
    <div class="form-group">
        <input type="text" name="username" class="form-control" id="username" placeholder="User Name">
    </div>
    <div class="form-group">
        <input type="text" name="email" class="form-control" id="email" placeholder="Email">
    </div>
    <div class="form-group">
        <input type="password" name="password" class="form-control" id="password" placeholder="Password">
    </div>
    <div class="form-group">
        <input type="password" name="confirm_password" class="form-control" id="confirm-password" placeholder="Confirm Password">
    </div>
    <div class="form-group pull-right">
        <button type="submit" id="register" class="btn btn-primary">Register</button>
    </div>
</div>
<?php echo form_close(); ?>

您能帮我解决这个错误吗?

【问题讨论】:

  • Users/actionRegister 路由在application/config/routes.php 中定义了吗?

标签: php codeigniter


【解决方案1】:

在这种情况下,您可以检查是否在控制器上加载了帮助表单,应该是这样的:

$this->load->helper('form');

在您的代码&lt;?php echo form_open('Users/actionRegister'); ?&gt; 上,Users/actionRegister 可以在 routes.php (this file is located under application/config) 上定义,以便于访问您的函数。你可以这样做:

$route['register'] = 'Users/actionRegister';

为了调用定义的函数,您现在可以在 form_open 函数中调用base_url().'register'

请阅读更多关于Configuring base_url in CI

在此之前,您必须编辑您的 .htaccess 文件,以便在 CI 中识别您的路线。 像这样:

<IfModule mod_rewrite.c>

RewriteEngine On

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

我们会看看你能不能解决这个问题。

【讨论】:

    【解决方案2】:

    首先你应该参考CodeIgniter user_guide来正确命名你的文件

    创建一个控制器 (application/controllers/Users.php) 并在其中发布一些代码:

    <?php 
    Class Users extends CI_Controller(){
        public function __construct(){
            parent::__construct();
        }
        public function index(){
            echo '<code>users/index/</code> is called';
        }
    
        public function actionRegister(){
            if($_POST){
                var_dump($_POST);
                }else{
                echo 'direct access to this function is not allowed';
            }
        }
    }
    

    然后像localhost/codeigniter/users/actionregister/这样直接访问您的网址

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-09
      • 1970-01-01
      相关资源
      最近更新 更多