【问题标题】:php echo base_url() bringing me to 404php echo base_url() 将我带到 404
【发布时间】:2016-11-20 18:34:36
【问题描述】:

我正在关注这个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();
    }

}

 ?>

查看_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!';
}


}
?>  

【问题讨论】:

  • 你不需要用?&gt;关闭控制器和模型

标签: php codeigniter


【解决方案1】:

配置应该是

$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 中启用了重写规则

【讨论】:

  • 应该是http://localhost/folder/Controller_ca还是http://localhost/folder?你能再解释一下第二部分吗?我把那个代码放在哪里,我查了文档,但没有说。
  • @carbon_unicorn 基本网址应类似于 $config['base_url'] = 'http://localhost/projectname/';
  • @wolfgang1983 谢谢
  • 谢谢大家的帮助!
猜你喜欢
  • 2016-12-02
  • 1970-01-01
  • 1970-01-01
  • 2016-12-22
  • 2015-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-24
相关资源
最近更新 更多