Codeigniter 中出现错误
我正在我的 codeigniter 应用程序中自动加载一个名为“render”的库。 该库的完整代码是:
class Render extends CI_Controller { public function template($template, $view, $extra_css, $extra_js) { $data = array(); if (isset($view)) { $data['view'] = $view; } if (isset($extra_css)) { $data['extra_css'] = $extra_css; } if (isset($extra_js)) { $data['extra_js'] = $extra_js; } $template = $this->load->view("templates/$template", $data, TRUE); echo $template; } }
该库工作正常,但问题是每当我手动加载该库或通过编辑自动加载文件时,当我在任何控制器中加载任何模型时都会收到错误。
A PHP Error was encountered Severity: Notice Message: Undefined property: Home::$my_model_name Filename: controllers/home.php Line Number: 11
这里第 10 行和第 11 行是:
$this->load->model('my_model_name'); $this->my_model_name->my_model_method();
我也尝试使用:
$this->load->model('my_model_name', 'My_model'); $this->My_model->my_model_method();
我的控制器“home”代码是:
class Home extends CI_Controller { function __construct() { parent::__construct(); } public function index() { $this->load->model('my_model_name'); $this->my_model_name->index(); } }
我尝试向我的库添加 __construct() 方法,但仍然没有运气。
i'm autoloading a library named as 'render' in my codeigniter app.
the full code of the library is :
class Render extends CI_Controller { public function template($template, $view, $extra_css, $extra_js) { $data = array(); if (isset($view)) { $data['view'] = $view; } if (isset($extra_css)) { $data['extra_css'] = $extra_css; } if (isset($extra_js)) { $data['extra_js'] = $extra_js; } $template = $this->load->view("templates/$template", $data, TRUE); echo $template; } }
this library working fine but the problem is that whenever i load this library manually or by editing autoload file, i get an error when i load any model in any of my controller.
A PHP Error was encountered Severity: Notice Message: Undefined property: Home::$my_model_name Filename: controllers/home.php Line Number: 11
here line number 10 and 11 are :
$this->load->model('my_model_name'); $this->my_model_name->my_model_method();
and i also tried to use :
$this->load->model('my_model_name', 'My_model'); $this->My_model->my_model_method();
My controller "home" code is :
class Home extends CI_Controller { function __construct() { parent::__construct(); } public function index() { $this->load->model('my_model_name'); $this->my_model_name->index(); } }
i tried to add a __construct() method to my library but still no luck.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过这样做,
您不是在创建一个库,而是一个控制器!为了创建一个库,只需创建该类并将其放入库文件夹中即可。
在你的库中,如果你想使用 CI 的加载器来加载模型,例如,你需要实例化主 CI 类。
类似于 (file
application/libraries/render.php
) :然后您可以
$CI->load
您的库、模型、其他库等中想要的所有内容。有关详细说明,请参阅利用库中的 CI 资源。然后,您可以按照通常的方式调用您的库,
$this->load->library('render')
,然后$this->render->whatever();
By doing
you're not creating a library, but a controller! In order to create a library, just create the class and put it into the libraries folder.
Inside your library, if you want to use CI's loader to load models, for ex., you need to instantiate the main CI class.
Something like (file
application/libraries/render.php
) :Then you can
$CI->load
everything you want inside your library, models, other libraries, whatever.See Utilize CI resource within your library for a thorough explanation of that. The, you call your library the usual way,
$this->load->library('render')
and then$this->render->whatever();