Welcome Guest, Not a member yet? Register   Sign In
Calling a controller within a controller (no URL redirect)
#1

[eluser]Tjunction[/eluser]
Is this possible at all? Not in the redirect the user to a different URL sense, but to call a method on a different controller, receive the response, use this in the same method and then echo the result out to the user.

I have require_once'd the controller in question, then added the following;

Code:
$runsheet_controller = new Runsheet_controller();

But this errors out with the following -

A PHP Error was encountered
Severity: Notice
Message: Undefined property: Runsheet_controller::$controller_session
Filename: job/Runsheet_controller.php
Line Number: 104

I've had a bit of a look over the load function of CI etc, but as far as I can see it can't load a controller... any help would be much appreciated Smile
#2

[eluser]theprodigy[/eluser]
It's typically not a good idea to call the functions of one controller in another. If you need public access to functions, it might be worth looking into putting the code in a library or helper, and loading it that way.
#3

[eluser]Tjunction[/eluser]
Thanks for the response!

In this instance I don't think it is worth creating as a library or helper, the method I want to use in question really belongs to the other controller (create_runsheets on runsheet_controller) and it's just a once off on a schedule controller.

The schedule_controller needs to pass an array of runsheet_items it has created to the runsheet_controller, which then groups the items by region and creates a runsheet for each.

Is it possible at all?
#4

[eluser]nelson.wells[/eluser]
You could try some clever trickery with redirects, flash data, and conditionals, but I'm leaning to what prodigy said. Encapsulate the functionality into a library or helper and then you can call it from wherever you need.
#5

[eluser]theprodigy[/eluser]
Quote:In this instance I don’t think it is worth creating as a library or helper, the method I want to use in question really belongs to the other controller (create_runsheets on runsheet_controller) and it’s just a once off on a schedule controller.

The schedule_controller needs to pass an array of runsheet_items it has created to the runsheet_controller, which then groups the items by region and creates a runsheet for each.
Is the runsheet_controller ever called as a controller, or is it just there to assist the schedule_controller? I don't think CI allows calling one controller method from a different controller. The only ways I can think of doing it is to setup your app with one of the modular extensions (HMVC or Matchbox) which allow calling a module controller from a different module, or just cURL-ing your array to the full domain path of that controller.
#6

[eluser]Tjunction[/eluser]
Yeah the runsheet_controller has it's own view, JS panel's etc, it just sucks that this one and only time I need to call it from a different controller I can't :p

Oh well. I guess I'll go with the library idea, thanks for your advice everyone, much appreciated Smile
#7

[eluser]wiredesignz[/eluser]
Modular Extensions PHP5 may be what you're looking for here. (see my signature)

Try using forum search before posting when you have a problem too, this topic has been discussed many times.
#8

[eluser]FranckRR[/eluser]
In the MVC paradigm, controllers respond to a user action (through a submitted form or an url). It must do only that !

Any other actions (DB access set apart), must be in classes or functions (thus library or helper).

The normal (and only one) flow is :
one action requested
-> one controller perform the action by using models to get data access, work on them
-> call a new view to display the result of the action

But there are case where within a controller, one must perfom another action. For instance from a list of anything, when a new instance is created, once done, we would like to request a list refreshing.
2 choices :
- display an intermediate view : instance created
- redirect ('list action', 'location') which performs a back & forth to the client




Theme © iAndrew 2016 - Forum software by © MyBB