简体   繁体   中英

Codeigniter form_open 404

After searching for hours I still don't get why I get a 404 error when I do a form_submit.

My controller looks like this:

class pages extends CI_Controller{

function view($page = 'home'){

    $this->load->helper('url');
    if(!file_exists('application/views/pages/'.$page.'.php')){
        show_404();
    }

    $data['title'] = $page;
    $this->load->view('templates/view',$data);
}

public function login_validation(){

}

The view page:

<?php echo form_open('pages/login_validation');

echo form_input(array(
'name' => 'firstname',
'placeholder' => 'Voornaam',
'class' => 'form-control input-lg',
'tapindex' => '1'
));

echo form_submit(array(
'name' => 'login_submit',
'value' => 'Register',
'class' => 'btn btn-primary'
));?>

.htaccess (in the root folder):

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|indexcp\.php?|resources|robots\.txt)
RewriteRule ^([A-Za-z0-9_/.-]+)$ index.php?/$1

I have this line in the autoload.php:

$autoload['helper'] = array('form','url');

In config.php:

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

routes.php:

$route['(:any)'] = "pages/view/$1";
$route['default_controller'] = "pages/view";
$route['404_override'] = '';

What is wrong with this configuration?

The problem you are having is with the following route (I am assuming you've posted the whole of your routes file):

$route['(:any)'] = "pages/view/$1";

This means that any file you load is going to go to the pages controller, call the function view, and pass in the parameter which is as $1. In this case, it's going to pass through either 'pages/login_validation' or 'login_validation' (I'm not 100% sure which, and I can't test at the moment.

Your view function is checking to see if the file 'application/view/pages/login_validation.php' or 'application/view/pages/pages/login_validation.php' exists, and when it can't find it, it is giving the 404 page.

To fix this, you can add the following into the routes.php file (above the $route[(:any)] = 'pages/view/$1'; line):

$route['pages/login_validation'] = 'pages/login_validation';

That will explicitly check that the page wanting to be loaded is the login_validation one, and then call that function within the pages controller.

If you don't want to keep adding routes each time, then you could modify the view function in pages to check if the $page variable matches the name of a function, and if so then call that function:

if (function_exists($page))
{
    $page();
}

The trouble you might have then is that you won't easily be able to pass through additional parameters to the given page. You might also accidentally name a page the same as a function in PHP, and then it will try and call that instead of loading the page you intend.

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