Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

Loading library/model classes from your views in CodeIgniter



Hi,

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….

$this->load->library(‘some_library’);

$this->some_library->some_library_function(some_parameters);

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();
$ci_obj->load->library('some_library');
$ci->some_library->some_library_function();

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

function load_library($library_name) {

$ci = & get_instance();
$ci->load->library($library_name);
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 comments

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.

  4. Fanatastic post! Makes me change my embargo on commenting

  5. 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.

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

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

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

  9. 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.

  10. 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.

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

  12. 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

  13. 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.

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

  15. You need to really control the remarks listed here

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

    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.

  16. 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 .

  17. 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.

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

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

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

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

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

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

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

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

  26. 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

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

  28. Whenever I originally commented I clicked the

  29. 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.

  30. 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.

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

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

  33. 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.

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

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

  36. 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.

  37. 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.

  38. 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.

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

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

  41. 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!

  42. 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.

  43. 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.

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

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

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

  47. 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.

  48. 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

  49. 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.

  50. 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.

  51. 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

  52. 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.

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

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

  55. Love your blog!

  56. Love your blog!

  57. Great post!

  58. Love your blog!

  59. Great post!

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

  61. Awesome writing style!

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

  63. Love your blog!

  64. Love your blog!

  65. Love your blog!

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

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

  68. 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.

  69. Love your blog!

  70. cool story bro

  71. 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!

  72. 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.

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

  74. 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

  75. 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: