我有一个名为的库文件 myMenu.php
<?php
class myMenu{
function show_menu()
{
$obj = & get_instance();
$obj->load->helper('url');
$menu = "<ul>";
$menu .= "<li>";
$menu .= anchor('books/index','List of books');
$menu .= "</li>";
$menu .= "<li>";
$menu .= anchor('books/input','Books entry');
$menu .= "</li>";
$menu .= "</ul>";
return $menu;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我在控制器中加载了这个库 books.php
function index()
{
$this->load->library('myMenu');
$menu = new myMenu;
$data['menu'] = $menu->show_menu();
$this->load->view('main_view',$data);
}
Run Code Online (Sandbox Code Playgroud)
但页面显示错误An error occurred : Unable to load the requested class: mymenu.为什么这个错误显示类名 mymenu(全部为小写),其中我myMenu在控制器中写道
ash*_*ina 13
两个问题:
1)您的命名约定是错误的.
在CodeIgniter中,库必须以大写字母开头.类名和文件名都必须以大写字母开头,并且必须匹配.请参阅以下文档. https://www.codeigniter.com/user_guide/general/creating_libraries.html
2)你不应该用myinsten实例化myMenu new.访问您加载的库时,这通常是常用的方法:
$this->load->library('mymenu'); // when calling the loader, the case doesn't matter
$data['menu'] = $this->mymenu->show_menu(); //'mymenu' is the lowercase of the class name
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23418 次 |
| 最近记录: |