oldblueday - 12 August 2010 10:19 PM
or is there another way to do this? What about using the
redirect('different_controller/function');
function?
Thanks-Rahul
Without getting into a large conversation about Web design, SEO best practices, etc., let’s just say, you probably want to use redirects in this case. A URL is a path to a resource. If authentication fails for that resource, you should redirect to a page that can provide a way to properly authenticate. It should not be the same path as the legitimate resource. Just makes sense to me (besides many practical technical reasons.)
Okay, I have to chime in on this controller-from-another-controller issue. Someone wrote that models should be database-centric. The CodeIgniter folks intended this, it’s true (see the manual), but it’s not necessarily the purpose of a model in the MVC architecture. According to the wiki:
Many applications use a persistent storage mechanism such as a database to store data. MVC does not specifically mention the data access layer because it is understood to be underneath or encapsulated by the model. Models are not data access objects; however, in very simple apps that have little domain logic there is no real distinction to be made.
Further wiki explanation of a model:
The model is used to manage information and notify observers when that information changes. The model is the domain-specific representation of the data upon which the application operates. Domain logic adds meaning to raw data (for example, calculating whether today is the user’s birthday, or the totals, taxes, and shipping charges for shopping cart items).
So a model, in essence, is meant to manage the information in an application. Not a controller. A controller is meant to simply retrieve data and hand it off to views to present it. A popular phrase for this notion of pure controllers is, “Fat Models and Skinny Controllers.” It means that your models should concern themselves with manipulating all the information of an application, and controllers concern themselves with simply grabbing data from models and passing it to views for displaying the information (now I’m getting redundant.)
I agree with the statement someone in this thread made that if you have to depend on calling controller methods from another controller, you have implemented bad MVC design. Why? Because if you stick to this notion of thin controllers, fat models, all of your information logic is modular. And we all know that, in programming, modularity is a good thing. 
Now, this is all theory and best practices. So let me finally get to my point. The constructor for the core Model class in CodeIgniter calls one lonely method: _assign_libraries. When we look at the documentation for _assign_libraries, we get a little revelation about the power of using fat models:
* Creates local references to all currently instantiated objects
* so that any syntax that can be legally used in a controller
* can be used within models.
Did you see that? ...any syntax that can be legally used in a controller can be used within models (you can call $this->db in your controllers. Models have no special “model sauce.”) Why is this better, than say, a library? Well, an application library in codeigniter is a pure class. That is, more often, it inherits from nothing. Sure, we can extend core classes, but you do not have to extend anything typically. In fact, the manual page for libraries shows an example of the ‘ole get_instance() call for getting framework resources:
$CI =& get_instance();
In a model, you have the same scope as you do in a controller. This makes it a good place for much of the logic that tends to bloat controllers (Heaven knows I’m guilty of bloating my controllers too.) Incorporating your information logic in models has the added benefit of making your “rough” procedural code absolutely modular. That translates to: No need to call controller methods from other controllers! Yeah!
Don’t get my wrong. For black-box functionality, libraries are very useful and proper. Anytime you need to use the loader class to get a “thing” that is independent and separate from the procedural nature of the application, use a library. Because models have the same access as controllers to framework resources, you can even call your libraries in your models to be used as aides to help you assemble your application information. 