php echo base_url() bringing me to 404

2019-08-04 14:36发布

问题:

I am following this tutorial to make a form to insert data to my database. I have come to a problem where my <?php echo base_url()?> is not linking to the form_validation page as it should. I have the config.php set to $config['base_url'] = ''; and the autoload.php set to $autoload['helper'] = array('url', 'form', 'html'); So I don't understand why I am getting the 404 error. Any help would be greatly advised.

Model_ca.php

<?php

class Model_ca extends CI_Model {

function __construct()
    {

    parent::__construct();
    }

}

 ?>

View_ca.php

 <html>
     <head> 
     <title>Add Chipper to Database</title>
     <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
      rel="stylesheet">
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
      </head>
     <body>
         <div class="container">
             <h3 align="center">Add Chipper to Database</h3>
             <!--Link form to base url which is defined in autoload.php-->
             <form method="post" action="<?php echo base_url()?>Controller_ca/form_validation">

               <div class="form-group">  
             <label>Enter Name</label>    
                <input type="text" name="name" class="form-control"/>
                </div>

                <div class="form-group">
                <label>Enter Location</label>    
                <input type="text" name="location" class="form-control"/>
                </div>

                <div class="form-group">
                <label>Enter Description</label>    
                <input type="text" name="description" class="form-control"/>
                 </div>

                 <div class="form-group">
                 <input type="submit" name="insert" value="Add Chipper" class="btn btn-info"/>
                 </div>

             </form>
         </div>

     </body>
 </html>

Controller_ca.php

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Controller_ca extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('url');

    }

    public function index() 
    {  
        $this->load->view('View_ca');
    }    


public function form_validation()
{
    echo 'Form Validation is Working!';
}


}
?>  

回答1:

Config Should be

$config['base_url'] = 'http://localhost/path/to/folder/';

.htaccess should be

RewriteEngine on
RewriteCond $1 !^(index\.php|assets|image|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

Make sure rewrite rule is enabled in php.ini