Nitish
Nitish

Reputation: 2763

Codeigniter unable to load library class

I have a library file named 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;
    }
}

Now I loaded this library in my controller books.php

function index()
    {
        $this->load->library('myMenu');
        $menu = new myMenu;
        $data['menu'] = $menu->show_menu();
        $this->load->view('main_view',$data);
    }

But the page shows error An error occurred : Unable to load the requested class: mymenu. Why this error is showing class name as mymenu (all in lowercase) wherein I wrote myMenu at controller

Upvotes: 3

Views: 25593

Answers (2)

ashiina
ashiina

Reputation: 1006

Two problems:

1) Your naming convention is wrong.

In CodeIgniter, libraries must start with a capitalized letter. The class name and file name both have to start with capital letters, and they have to match. Refer to the document below. https://www.codeigniter.com/user_guide/general/creating_libraries.html

2) You shouldn't instantiate myMenu with new. When accessing a library which you loaded, this is pretty much the usual way:

    $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

Upvotes: 13

Willem Ellis
Willem Ellis

Reputation: 5016

Class name should be capitalized. class MyMenu

Upvotes: 1

Related Questions