Call a controller from a library |
Hi guys,
some day ago I start to learn CI and a bit of MVC framework. I follow this tutorial on youtube about login system (using ion_auth was too difficult for me now). While I follow the tutorial I've made some changes to let this compatible with CI3, correct some bug like xss_clean, ad adapt to my need. Everything works, of course its not awesome but it works. But, I want to learn I hate writing code and don't understand. So, I read that Controller in MVC paradigm, have to be used for build pages, load views, etc. I don't like to have function like "form validation" in controller. So I try to build a library with all function like logout, registration, login, validation, but I don't understand how I could let the library works. My function on library call specific controller function, how can I call that function on controller but let that independent? so I could maybe think to reuse the library for other small project of mine? thank you all in advance
You shouldn't do this ... calling a controller from a library is the complete opposite of making it independent.
Controllers are application-specific, libraries are not - that's the main difference between them. When you call a controller from within a library, that makes the controller a dependency for your library, and that in turn means that your library is no longer portable. (01-29-2016, 03:45 AM)Narf Wrote: You shouldn't do this ... calling a controller from a library is the complete opposite of making it independent. mmm =( you right...you damn right...so I have to loose my intention to write a library =( to do an example I have this function PHP Code: public function login_validation(){ where members(); and login(); that you see at the end are function of the same controller. With a code like this there is no way to make a library... Quote:With a code like this there is no way to make a library... Libraries sound cool but much of the time Models are the better solution. when you are first developing start by just working in the controller. its faster to get something working that way. then refactor the code into your model(s). Use application/config/autoload.php right away so you don't have to wonder if some library or helper has been loaded or not, like PHP Code: $autoload['libraries'] = array('database', 'form_validation','session','table', 'email'); |
Welcome Guest, Not a member yet? Register Sign In |