Welcome Guest, Not a member yet? Register   Sign In
Library vs. Controller
#1

[eluser]pistolPete[/eluser]
I want to ask a general question:

In my application I have several controllers (of course) and some libraries, for example a Layout library and a Login library.
I also could put that code into my subclasses (Frontend_Controller, Backend_Controller) of the Controller class which reside in MY_Controller.php.
Now I want two ask, if there is any advantage in having a Library over extending the controllers.

Thanks in advance,

Pete
#2

[eluser]roj[/eluser]
I guess the basic idea is that libraries are classes which are shared and available throughout the application while controllers are primarily the glue between models and views.
#3

[eluser]dmiden[/eluser]
Basically I use the libraries for classes that are used on all sites,
f.ex. I have a User library which handles login/logout/userdata etc.
This library has public $vars that are assigned when the class initialized.
So in my controllers I can f.ex. do this
Code:
function index()
{
   if ($this->user->authed == FALSE)
   {
      redirect('login/');
   }
}

The models are used for actions that arent used on all controllers.
F.ex. admin_model which handles all administration.
#4

[eluser]pankaj[/eluser]
lets assume that we don't have a validation library then what you will do for validation you will write all your validation code in you corresponding controller. and this will be a mess if you add hole validation code in one controller. that's why why libraries are created so that you don't need to do all coding in your controller and besides that you can access that libraries throughout your application as this is called code reusability.
this is the big advantage of MVC patterrn.
#5

[eluser]JoostV[/eluser]
The advatage of having a Library over extending the controllers is
- portability
- you only call it when you need it
#6

[eluser]xwero[/eluser]
I think you shouldn't see it as the advantages of a library over extending controllers. You have to decide if the code you are about to write is application specific or not. And if you think it's reusable you abstract it in order to make it as flexible as possible. If you find out that because of the abstractions you have to use too many methods or add too many layers, that is the sign the code is application specific.

But before you start extending the controllers see it hooks aren't possible. For a login and layout library this is certainly an option.




Theme © iAndrew 2016 - Forum software by © MyBB