简体   繁体   中英

How to fix requested URL not found error in Codeigniter

I am new to codeigniter and I am trying to build a registration page using it. When I am calling a function in the controller the server returns "The requested URL /Users/actionRegister was not found on this server" error.

I changed the .htaccess file and rewrite_mode on apache modules was started.

<?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(); ?>

Could you please help me to fix this error?

In this case you can check if load the helper form on your controller, this should be like this:

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

On you code <?php echo form_open('Users/actionRegister'); ?> <?php echo form_open('Users/actionRegister'); ?> the Users/actionRegister can be defined on routes.php ( this file is located under application/config ) for convenient access of your function. You can do that like this:

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

In order to call the defined function, you can now call base_url().'register' inside form_open function.

Please read more about Configuring base_url in CI

Before that, you must edit your .htaccess file in order to recognize your routes in CI. Like this:

<IfModule mod_rewrite.c>

RewriteEngine On

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

We'll see if you get through this.

first of all you should refer to CodeIgniter user_guide to name your files correctly

create a controller ( application/controllers/Users.php ) and post some code in it:

<?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';
        }
    }
}

then use direct access to your url like this localhost/codeigniter/users/actionregister/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM