Should I use my models in a library?
I've got my auth library in codeigniter which accesses the database to check to see if the email/password combination is correct.
If I'm sticking to the MVC practice, should I move the function that interacts with the database to my model, or is it best practice to leave it where it is so I can use it in the future?
Doesn't make much difference to me, other than the fact I'd have to re-writ开发者_开发技巧e the library and create the function in my model, but if that's the way it should be then so be it.
Well generally the rule of thumb is to have all functions that are involved with processing data and performing database queries go in the model. Different developers have different methods, but I think you shouldn't break the MVC model and put database code in your controllers.
I've been developing an auth library myself called WolfAuth for anyone to fork and contribute too / use: https://github.com/Vheissu/WolfAuth-for-Codeigniter-2.0-
As you can see in my model I have a get user function which can accept a needle and haystack value. So I can write a whole bunch of different functions in my library for getting a user by ID or logging a user in by calling this model function with different parameters instead of calling different functions.
In WolfAuth you can see that all functions will call the model function get_users with different values. I believe having one function that can retrieve multiple pieces of data instead of writing separate functions is much cleaner than having 6 functions when one function can do everything those 6 functions can do.
So to answer your question: write the function that interacts with the database in your model, but keep the same function in your library to call the model function and return its value.
Also, if you want feel free to take pieces of code and borrow ideas from my auth library and use them in yours if it helps you understand breaking code up better.
精彩评论