php echo base_url()带我到404

时间:2016-11-20 18:34:36

标签: php codeigniter

我正在关注此tutorial以创建一个表单以将数据插入到我的数据库中。我遇到了一个问题,我的<?php echo base_url()?>没有链接到form_validation页面。我将config.php设置为$config['base_url'] = '';并将autoload.php设置为$autoload['helper'] = array('url', 'form', 'html');所以我不明白我为什么会收到404错误。任何帮助都会得到很好的建议。

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 个答案:

答案 0 :(得分:2)

配置应该是

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

.htaccess应该是

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]
  

确保在php.ini中启用了重写规则