You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter插入数据报未捕获异常:调用未定义函数site_url()

问题背景

实现向数据库插入数据的功能时,在application/controller目录下创建了控制器文件baby_form.php,代码如下:

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

class Baby_form extends CI_Controller{
    public function index(){
        $this->load->view("baby_form_add");
    }
    function savingdata(){
        // 获取视图页面提交的数据
        $data = array(
            'name' => $this->input->post('name'),
            'meaning' => $this->input->post('meaning'),
            'gender' => $this->input->post('gender'),
            'religion' => $this->input->post('religion')
        );
        // 将数据插入数据库baby表
        $this->db->insert('baby',$data);

        redirect("baby_form/index");
    }
}
?>

对应的视图文件baby_form_add.php代码如下:

<!DOCTYPE html>
<html>

<head>
    <title>Baby Form Add</title>
</head>

<body>
    <form method="post" action="<?php echo site_url('baby_form/savingdata'); ?>">
        <table>
            <tr>
                <td>Name:</td>
                <td>:</td>
                <td><input type="text" name="name"></td>
            </tr>
            <tr>
                <td>Meaning:</td>
                <td>:</td>
                <td><input type="text" name="meaning"></td>
            </tr>
            <tr>
                <td>Gender:</td>
                <td>:</td>
                <td><input type="text" name="gender"></td>
            </tr>
            <tr>
                <td>Religion</td>
                <td>:</td>
                <td><input type="text" name="religion"></td>
            </tr><br><br>
            <tr>
                <input type="submit" name="submit" value="Save">
            </tr>
        </table>

    </form>

</body>

</html>

访问页面时抛出未捕获异常,具体报错信息:

  • 异常类型:Error
  • 异常信息:Call to undefined function site_url()
  • 报错文件路径:C:\xampp\htdocs\codeigniter3\application\views\baby_form_add.php
  • 报错行号:第9行
报错原因

site_url()是CodeIgniter 3中URL助手提供的函数,框架默认不会自动加载该助手,直接调用就会触发未定义函数错误。另外控制器中使用的redirect()函数同样来自URL助手,不加载的话后续表单提交跳转时也会触发相同类型的报错。

解决方法

方法1:全局自动加载URL助手(推荐)

打开application/config/autoload.php配置文件,找到$autoload['helper']配置项,将url加入加载数组:

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

保存配置后刷新页面即可正常访问,后续项目所有位置都可以直接调用URL助手提供的函数,无需重复加载。

方法2:在对应控制器中按需加载

如果不需要全局加载URL助手,可以在控制器的构造函数中单独加载,当前控制器下所有方法都可正常使用相关函数,修改后的控制器代码如下:

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

class Baby_form extends CI_Controller{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
    }

    public function index(){
        $this->load->view("baby_form_add");
    }
    function savingdata(){
        $data = array(
            'name' => $this->input->post('name'),
            'meaning' => $this->input->post('meaning'),
            'gender' => $this->input->post('gender'),
            'religion' => $this->input->post('religion')
        );
        $this->db->insert('baby',$data);
        redirect("baby_form/index");
    }
}
?>

内容的提问来源于stack exchange,提问作者Shubham Bhattaniya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 09:48:53