开发者

CodeIgniter uses index.php

开发者 https://www.devze.com 2023-04-10 20:42 出处:网络
I have codeigniter installed in root/igniter folder. under root/igniter, I have the following .htaccess file:

I have codeigniter installed in root/igniter folder.

under root/igniter, I have the following .htaccess file:

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

Pages open just as I would expect them to without using 'index.php'. So, my home page lies at http://example.com/igniter and my registration page at http://example.com/igniter/registration opens as well.

However, in my registration form, the submit button which is handled by a CI controller redirects to

http://example.com/igniter/index.php/registration

I have made sure that my config.php under applications/config has

$config['index_page'] = "";

This is what my Registration controller looks like:

public function index()
    {
        $this->load->helper(array('form', 'url'));
        $this->load->model('Registration_Model');       
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        $this->form_validation->set_rules('userna开发者_如何学JAVAme', 'Username', 'trim|required|min_length[5]|max_length[12]|callback_username_check|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|max_length[12]|matches[passconf]');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback_email_check');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('registration');
        }
        else
        {
            //input data into database here
            $username = strtolower($_POST['username']);
            $password = md5($_POST['password']);
            $email = strtolower($_POST['email']);

            $data = array
                    (
                        'username' => $username,
                        'password' => $password,
                        'email' => $email
                    );

            $this->Registration_Model->addUser($data);  
            $this->load->view('registrationsuccess');
        }
    }

As far as I can see, I am not explicitly asking the controller to redirect to index.php/registration.


Use this in your .htaccess file:

RewriteEngine on

RewriteCond $1 !^(index\.php|robots\.txt|images|css|js|user_guide)
RewriteRule ^(.*)$ /index.php/$1 [L]

Note the RewriteRule line in my code. There is no ? mark as you used. In RewriteCond you may use as many folders as you like and the mentioned folders have access. CodeIgniter can not access the resource from any folder that is not listed in RewriteCond here.


You should also point your index method at the method you want to receive it. For example, if your class is "Registration" your index method should look like this:

public function index()
{
    this->registration();
}

Then do everything in the method called "registration." This will help keep things clear for other developers who might work on this code in the future.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号