Loading library/model classes from your views in CodeIgniter


When I was new to CodeIgniter, I need some data manipulation using a library function in my view classes, I simply loaded the library and called the library function, as I always do….



but when run this code, I surprised by the error:

Before this I had used library functions many times in the code, and it always works fine, and after checking the code thoroughly I didn’t find any mistake in the code.

So, what happened with that code?

then I realize that I always use the library functions in the Controllers, this is the first time I’m using it in the views.

then I found the problem! You can do almost everything in the view, even when you shouldn’t, but loading library, models etc. has to be done in the controller. After searching and reading many articles on the net I found a solution to use library functions anywhere in the application with the help of get_instance() method.

get_instance() method returns the CodeIgniter super object. this is used to access CodeIgniter native resources from within your custom classes. Normally, to call the codeigniter’s member function we use $this object i.e. $this->load->library(‘some_library_name’); or $this->load->helper(‘some_helper_name’);

but if you want to use your these functions from views, or any other custom class you have to use &get_instance() method to get the codeigniter’s object.

$ci_obj = & get_instance();

or you can use the given helper function to load library from anywhere in the code:

function load_library($library_name) {

$ci = & get_instance();
return $ci->$library_name;


it will return the library object and you can call any library function using that object in anywhere in the views or any other custom class.



77 pings

Skip to comment form

  1. We appreciate you all of your time & operate. As a Newcomer, I will be consistently performing a search online relating to content material articles that could be involving aid me.

  2. I do not even know how I ended up here, but I thought this post was great. I don’t know who you are but certainly you’re going to a famous blogger if you are not already 😉 Cheers!

  3. I like this weblog so much, saved to my bookmarks.

    • Lawanda on March 30, 2012 at 10:20 AM

    Fanatastic post! Makes me change my embargo on commenting

  4. I was just looking for this information for some time. After 6 hours of continuous Googleing, at last I got it in your website. I wonder what is the lack of Google strategy that do not rank this kind of informative websites in top of the list. Usually the top sites are full of garbage.

    • Rayford on March 30, 2012 at 7:52 AM

    comments really should often be made whenever you read some thing as very good as this, thanks a million

  5. Made my day once again, wonderful post..two thumbs up!:)

  6. A well written story. I’m always looking for news like this.

  7. As a site owner I believe the material here is reallyfantastic. I appreciate it for your efforts. You should keep it up forever! Best of luck.

    • url on March 28, 2012 at 12:21 PM

    Heya, I just hopped over to your webpage via StumbleUpon. Not somthing I might typically browse, but I appreciated your thoughts none the less. Thank you for making something well worth reading through.

  8. How did you make a blog site look this awesome. Email me if you get the chance and share your wisdom. Id be appreciative.

  9. There are some interesting points on time in this article nonetheless I don’t know merely see every one of them center to help heart. There exists some quality but I most certainly will take have opinion until eventually I investigate it further more. Good write-up , thanks therefore we want much more! Added to FeedBurner also

    • Dawfydd on March 28, 2012 at 9:37 AM

    I just need to notify you that I’m extremely new to blogging and seriously savored you might be webpage. Possible I’m possible to bookmark your site . You actually have remarkable reports. Thank you a bunch for sharing with us your webpage.

  10. I will immediately grab your rss feed to stay informed of any updates.

  11. You need to really control the remarks listed here

    • 5s on February 5, 2012 at 8:41 PM

    Wonderful goods from you, man. I have understand your stuff previous to and you’re just too fantastic. I actually like what you have acquired here, certainly like what you’re stating and the way in which you say it. You make it enjoyable and you still take care of to keep it smart. I cant wait to read far more from you. This is really a terrific web site.

  12. Hi my family member! I wish to say that this post is amazing, great written and come with approximately all vital infos. I would like to look more posts like this .

  13. Good day! Do you use Twitter? I’d like to follow you if that would be ok. I’m definitely enjoying your blog and look forward to new updates.

  14. Some genuinely select articles on this site, saved to bookmarks .

  15. I really like what you are saying, and am glad I found your website. Please keep it up.

  16. Very great visual appeal on this internet site , I’d rate it 10 10.

  17. I love your wp format, where did you get a hold of it?

  18. I like this blog very much, Its a really nice place to read and obtain info .

  19. I believe other website proprietors should take this website as an example , very clean and excellent user genial design .

  20. Brilliant, thank you, I will bookmark you now!

  21. I gotta favorite this web site it seems very useful very useful

  22. Amazing article admin thank you. I observed what i used to be looking for here. I’ll review complete of posts in this time of day

  23. Locating dependable locksmith professional professional is an excellent means of preserving your current locks.

    • VICTOR on January 27, 2012 at 3:27 AM

    Whenever I originally commented I clicked the

  24. Spot on with this write-up, I truly think this website needs far more consideration. I’ll most likely be once more to learn way more, thanks for that info.

  25. Howdy very cool web site!! Man .. Excellent .. Superb .. I’ll bookmark your blog and take the feeds additionally…I am glad to search out so many helpful information here in the post, we’d like work out extra techniques in this regard, thank you for sharing.

  26. woh I am glad to find this website through google.

  27. As soon as I observed this internet site I went on reddit to share some of the love with them.

  28. Normally I do not read article on blogs, however I wish to say that this write-up very pressured me to take a look at and do it! Your writing style has been surprised me. Thank you, very nice post.

  29. I envy your work , thanks for all the good blog posts.

  30. I truly enjoy looking at on this internet site , it contains good content .

  31. Thanks for sharing excellent information. Your web site is so cool. I’m impressed by the details that you have on this blog. It reveals how nicely you perceive this subject. Bookmarked this web page, will come back for extra articles.

  32. Thanks for this distinct blog; this is the kind of thing that keeps me on track through my day. Been looking around for your blog after being sent to it from a buddy and was thrilled when I stumbled on it after googling for a while. As a dedicated blogger, I’m excited to see authors like you taking initivative and contributing to the community. Just commenting to show my appreciation for your work as it’s quite inspiring, and many bloggers do not get acceptance they deserve. I am sure I’ll visit again and will recommend to my friends.

  33. Aw, this was a really nice post. In thought I want to put in writing like this moreover – taking time and precise effort to make a very good article… but what can I say… I procrastinate alot and on no account seem to get one thing done.

  34. Have you thought about adding some differing opinions to your article? I think it might enhance everyones understanding.

  35. Brilliant, thanks, I will visit again soon!

    • kasyno on January 24, 2012 at 11:25 PM

    Good web site! I truly love how it is easy on my eyes it is. I’m wondering how I could be notified when a new post has been made. I’ve subscribed to your RSS feed which may do the trick? Have a nice day!

  36. Can I simply say what a aid to search out someone who actually is aware of what theyre talking about on the internet. You positively know tips on how to bring an issue to gentle and make it important. Extra people need to learn this and understand this side of the story. I cant believe youre not more widespread because you definitely have the gift.

    • kasyno on January 24, 2012 at 4:15 PM

    Just wish to say your article is as astounding. The clarity in your post is just cool and i could assume you are an expert on this subject. Fine with your permission let me to grab your RSS feed to keep up to date with forthcoming post. Thanks a million and please carry on the gratifying work.

  37. Thank you very a lot for writing this post. I enjoyed it.

  38. Definitely, what a fantastic site and educative posts, I surely will bookmark your website.All the Best!

  39. I’d forever want to be update on new blog posts on this web site , saved to fav! .

  40. Heya i am for the first time here. I found this board and I find It truly useful & it helped me out much. I hope to give something back and help others like you aided me.

  41. Hello there! Would you mind if I share your blog with my myspace group? There’s a lot of folks that I think would really enjoy your content. Please let me know. Thank you

  42. Hello there, I found your website via Google while looking for a related topic, your site came up, it looks great. I have bookmarked it in my google bookmarks.

  43. This is a great blog for finding articles on this topic. I know this information exists elsewhere, but you have done a great job condensing it down for all.

  44. There are some exciting points in time in this article nonetheless I don’t know merely see these center for you to heart. There may be some credibility but Let me take carry opinion right until I explore it further more. Good post , thanks so we want additional! Added to FeedBurner in addition

  45. I just want to mention I am just newbie to weblog and actually enjoyed this web-site. Almost certainly I’m likely to bookmark your website . You definitely come with fabulous articles and reviews. Bless you for sharing with us your blog.

    • debt on January 10, 2012 at 6:09 AM

    Awesome post! I will keep an on eye on your blog.

  46. You have raised quite a terrific details, respect to the write-up.

  47. Love your blog!

  48. Love your blog!

  49. Great post!

  50. Love your blog!

  51. Great post!

  52. This is by far my favorite blog! Keep up the good work!

  53. Awesome writing style!

  54. Finally! A blog worth reading! I bookmarked your blog!

  55. Love your blog!

  56. Love your blog!

  57. Love your blog!

  58. Great work! I wish I could write this well!

  59. Ones web page is definitely specifically valuable for myself. I bookmarked your site!

  60. Hey There. I found your page searching with yahoo. This is an extremely well written article. I will be sure to bookmark it and return to read more of your useful info. Thank you for sharing it. I will absolutely be back again.

  61. Love your blog!

  62. cool story bro

  63. I like reading blog posts, and when I stumbled upon to this weblog, it just blew me away! Hey there I mean it! Your contents are rich and I discover them very useful!

  64. Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information.

  65. This is the most influential thing I read all day.

  66. Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to more added agreeable from you! However, how can we communicate?

    1. you can contact me blog@girishgaurav.com

  67. Hey Gaurav…. Remember Facing this problem a lot of times. And yes the getinstance method was really really helful.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: