Can we call one controller function in other controll fucntion |
[eluser]MianUsman[/eluser]
Hi All, once again i am disturbing all of u, my problem is that i have 2 controller 1-Auction function test() 2-Property function test2() In Auction controller i want to call the Property function can i write this in Auction controller property->test2() Thanks in Advance. Regards.
[eluser]Dam1an[/eluser]
To do so, you would need to include the property controller, create a property object and call the method on that object (assuming its public) Mind if I ask why you'd rather do it this way then by creating a library or helper?
[eluser]MianUsman[/eluser]
This is big problem abt URL routing my url is www.abc.com/fr/property_test/category/product here property_test is the combination of the controller_method what i do i call default control and pass this argument i just simple split my argument and now need to call that method of that controller. one way is i extend other class and then by using $this->test2() call this this approach work but in this as u know function name can not be same bcz of inheritance so need some other solution,if u give some other idea i will be really very thankful to u Regards.
[eluser]Evil Wizard[/eluser]
simple answer: NO Longer answer: It was something I looked into because I thought I needed the functionality of another controller, I mean why re-invent the wheel? lol, I went into the guts of CI to add a "$this->load->controller('controller_name);" method, but if you extend the "controller" to provide more default functionality, like checking if a user is logged in, eventually you will end up with an error message like the following... Code: Call to member function userdata() on non object on line xx
[eluser]jdfwarrior[/eluser]
This comes through the forums kinda often. I would suggest you rethink the way you have some of your stuff structured. The way I do things, and what seems to be the appropriate way to do it, is you don't have functionality in the controller. Parsing data, manipulating data, loading or calling data from the database, etc should all be done in a library, helper, or a model. The controller should be reserved for basic tasks and then loading views/templates. Can you give us a little more info on what it is you are trying to accomplish? Why are you routing everything through the default controller like that? Why not just create the other controllers?
[eluser]wiredesignz[/eluser]
In FACT you can call one controller from another. Look into using Modular Extensions HMVC (see my signature) or Wick. However, it seems that you may benefit more by using a controller base class, having the common method within this class. You may like to check the user guide regarding using MY_Controller. Or search the forums.
[eluser]MianUsman[/eluser]
sir what i want to eliminate the function name from the controller and each control having around abt 10 function ,for test purpose i write rule in routing class $route['(en|sp|por|pol|chn)/auction/(:any)/(:any)'] = 'auction/info/$2/$3'; but problem is that how i can call other function in same controller like i have two function in AUCTION controller 1- info 2-detail but if i use my route which i write above how we identify which function i am calling Really need help ,i am working on it from 3 days didn't find solution Regards.
[eluser]jdfwarrior[/eluser]
I'm still confused. Language barrier, sorry. Are you trying to call another function that is in the same controller, or call a function in a different controller? From the first post, it seemed like you were trying to call a function from a different controller, not it seems like you are trying to call one from the same controller. To call another function that is in the same controller, you just do: Code: $this->function_name();
[eluser]Dam1an[/eluser]
[quote author="MianUsman" date="1242930480"] Code: $route['(en|sp|por|pol|chn)/auction/(:any)/(:any)'] = 'auction/info/$2/$3'; but if i use my route which i write above how we identify which function i am calling [/quote] You've hardcoded the function in your rerouted route, so you know its 'info' Alternatively, you can use Code: $this->router->method; Both will return the name of the function being executed... but I'm not sure how this helps?
[eluser]MianUsman[/eluser]
yes u right sir i am trying to eliminate the function name from the URL which is my requirement after long struggle i found 2 solution 1- in route class write rules but its have a problem www.abc.com/fr/auction/nayyer/123 this will be display on url at back end what i did i wrote url for this $route['(en|sp|por|pol|chn)/auction/(:any)/(:any)'] = 'auction/info/$2/$3'; but problem come here how i will call other function in the same controller my other fuction is Controller Auction function info() function test() if i use my route method , its always call INFO function not TEST function. 2- my second solution is to make a default controller name Nav and extend with Auction controller with this www.abc.com/en/auction_info/nayyer/123 that URL will be display in defualt class i get First argument split it first name is controller and second is function, i use $this->info() it will call with out change the URL. but in this solution there is another problem , i have to extend each controller with other and name of the function should be different bcz each perform different action. can u guide me what i do in this case Regards. |
Welcome Guest, Not a member yet? Register Sign In |