php codeigniter 返回“消息:未定义的属性:欢迎:: $load”尝试加载帮助程序库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21339820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
codeigniter returns "Message: Undefined property: Welcome::$load" trying to load helper lib
提问by dot
Background Information
背景资料
I just installed a fresh copy of CI and modified the welcome controller to include the url Helper so I can call the method base_url. I then try to call this method from home.php
我刚刚安装了 CI 的新副本并修改了欢迎控制器以包含 url Helper 以便我可以调用该方法base_url。然后我尝试从home.php
Problem:I'm getting the following error message:
问题:我收到以下错误消息:
Message: Undefined property: Welcome::$load
Filename: controllers/welcome.php
Code:
代码:
This is what my welcome controller now looks like:
这是我的欢迎控制器现在的样子:
class Welcome extends CI_Controller {
public function __construct()
{
$this->load->helper('url');
}
public function index()
{
$this->load->view('home');
}
}
The view looks like this:
视图如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge"/>
<meta charset="utf-8">
<meta name="viewport" content="width = device-width">
<meta name="description" content="">
<!-- Le styles -->
<title>test site</title>
<script>
var BASEPATH = "<?php echo base_url(); ?>";
</script>
<link href="<?php echo base_url('assets/css/bootstrap.min.css')?>" rel="stylesheet">
<link href="<?php echo base_url('assets/css/navbar.css')?>" rel="stylesheet">
</head>
The system is dying on the line in the controller's constructor where i try to load the library...
系统正在控制器的构造函数中死机,我尝试加载库...
What I've done so far:
到目前为止我所做的:
- Read the manual. https://www.codeigniter.com/user_guide/helpers/url_helper.html
Tried to include the url library in the config/autoload.php like so:
$autoload['helper'] = array('url');
- 阅读手册。https://www.codeigniter.com/user_guide/helpers/url_helper.html
尝试在 config/autoload.php 中包含 url 库,如下所示:
$autoload['helper'] = array('url');
But I'm still getting the error. Any suggestions?
但我仍然收到错误。有什么建议?
Thanks.
谢谢。
Screenshots:
截图:
回答by
You forgot a crucial thing;
你忘记了一件重要的事情;
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url'); //Loading url helper
}
public function index()
{
$this->load->view('home'); //Loading home view
}
}
The parent::__construct. If you don't do that; the Controller will not inherit it's abstract layer when you override the __construct in your own controller.
的parent::__construct。如果你不这样做;当您在自己的控制器中覆盖 __construct 时,控制器不会继承它的抽象层。
As long as you don't override your __constructit's all ok. It only happens when you override it. You don't have the loadfunctionality because the Welcome class is empty (no inheritance), even if it extends the CI_Controller(but with a __constructoverride).
只要你不覆盖你__construct的一切都可以。它仅在您覆盖它时发生。您没有该load功能,因为 Welcome 类是空的(没有继承),即使它扩展了CI_Controller(但具有__construct覆盖)。


