Welcome Guest, Not a member yet? Register   Sign In
How to call a function in another controller?
#1

[eluser]anna16[/eluser]
Hello guys

I have two controller

controller 1 --> site
controller 2 --> login

now i want to call the method logout from site controller.
the logout method is in login controller.

Do i need to load login controller from inside of site controller.

Sorry this is very noob question.
Can you show me how to do this.

Thanks in advanced.
#2

[eluser]Twisted1919[/eluser]
By default you cannot achieve this with CI, you would need to install HMVC in order to call a controller from other controller.
However, what you can do, is to create a library that handles login/logout/etc actions, and call it from each of your controllers/models, that way you share code between the controllers.
#3

[eluser]bretticus[/eluser]
To elaborate more on @Twisted1919's advice...

Controllers are meant more for controlling what happens when the user goes to a specific URL. Controller methods are not meant to be called by methods in other controllers. Using a library as @Twisted1919 suggests allows you to load an authentication library in multiple controllers and use it at the controller method level. If it seems confusing (and I know it is) think of controller/method as noun/action. For example, if you have pages that are for dealing with users specifically, you might have a user controller. Since users need to log in, you might most likely have a login method. Thus, you get user/login. The authentication library might have a feature that when you try to go to site/view_secured, as the user has not previously logged in, he or she gets redirected to user/login.

Good luck!
#4

[eluser]wiredesignz[/eluser]
Strictly speaking a controller manages input from the user and controls output to a view. It has nothing to do with a URL as such. The router and input classes take care of the interface to the controller from the outside world.

Controllers in an HMVC triad could care less how or why they are instantiated. They simply do a job.
#5

[eluser]bretticus[/eluser]
[quote author="wiredesignz" date="1286603111"]Strictly speaking a controller manages input from the user and controls output to a view. It has nothing to do with a URL as such. The router and input classes take care of the interface to the controller from the outside world[/quote]

True. Usually, I'm stricter on MVC Smile

Ruby on Rails, Code Igniter, etc are touted as MVC frameworks. One thing that they all have in common that's not really part of the MVC paradigm (and what's popular with developers) is the notion of Convention over Configuration. What this means is that, out of the box, the separate URL router component will automatically route to a controller/method based on some convention. In Code Igniter, that convention is that the controller class name and a method name (within the same class) make up the request URI. Of course you can configure your routes with the routing class. What I should have said is that Controllers, for Code Igniter, are meant more for controlling what happens when the user goes to a specific URL because of this Convention over Configuration. Strictly speaking, a controller is just for grabbing data (models) and shoveling it off to a layout (views.) Still, as has been mentioned, CI doesn't have any default manner for calling other methods in other controllers. Also, as mentioned, you need to install one of various third-party libraries to use HMVC if you want to call controller methods in your desired manner.




Theme © iAndrew 2016 - Forum software by © MyBB