Welcome Guest, Not a member yet? Register   Sign In
[Q]How to get instance of the Invoking Controller
#1

[eluser]vlad_ci[/eluser]
I plan to have a class essentially for every 'function'
in a controller

Then in the controller I will store some 'static' data that I want to share
amongst the URL handing classes.

(90% of the work on the CI side is to handle JSON requests form my AJAX client,
so I do not need to use the viewes)


One thing I cannot figure out -- is how to obtain the
a reference to the Controller that CI instanciated for a particular request

(since the function-instanciated class would not 'know' the instance it was called from)

I understand that I can pass a reference to the Controller into every class constructor,
but I would like not to do that (because it appears that the Controller instance will
be 'live' anyway for the length of the request)


Code:
class Start extends Controller
{

function hello($name)
{
  $handler=handlerFactory->get("hello");



try
{
  //how to get the instance of the 'Start' class within $handler?
  $handler->init($name);
  $handler->output();
}
catch (HandlerException $e)
{
}
catch (CIException $e)
{
}

}


thank you in advance
#2

[eluser]sophistry[/eluser]
use standard PHP object oriented code:
Code:
$this->other_method_in_controller_class();
#3

[eluser]vlad_ci[/eluser]
I did not understand:
Code:
$this->other_method...

is a function call..


What I was looking for is if I am within the method of the
$handler class instance
(say I am inside the 'init' function)

how do I get a hold of the instance of the controller

Is there a static method something like:

CI::getCurrentControllerInst()

?

thank you
#4

[eluser]Pascal Kriete[/eluser]
There is a function called get_instance that will give you a reference to the CI super object (which, at least in part, is the controller).
Code:
$CI =& get_instance();
#5

[eluser]vlad_ci[/eluser]
thank you, looked at

Code:
$CI =& get_instance();

But I cannot figure out how from there to get to the instance
of my controller. Is there a way?

I also looked at the Controller.php and Loader.php but could
not see that they had a pointer or a reference to the my specific
controller.

(I also thought of using a static function in my 'Start'
controller class -- but it cannot have access to
instance specific data members (obviously)
)


thank you
#6

[eluser]m4rw3r[/eluser]
get_instance() returns the instance of the first (usually the only) loaded controller.

Example:
Code:
class Foo extends Controller{
    var $bar = 'Foo';
    function index(){
        echo $this->bar;

        $CI =& get_instance();
        $CI->bar = 'bar';

        echo $this->bar;
    }
}

This should echo Foobar if I didn't mess up somewhere.
#7

[eluser]sophistry[/eluser]
@vlad_ci, i think you may be making this more complicated than it needs to be. you say you want "a class for every function in a controller"... why? what is the problem you are trying to solve by making a "class for every function." that is a strange design but you sound like you might know what you are doing so i am curious to know why you need this kind of architecture.

cheers.
#8

[eluser]vlad_ci[/eluser]
@m4rw3r - thank you, get_instance indeed returns my Controller, not a base
class as I thought previously. And it does exactly what I need.

@sophistry -- a class for every function because I am thinking
(and I am just a one month old as far as PHP/Javascrip/Ajax/codeigniter/
web development is concerned (I am from C++/Boost/STL/threading world)

a) I would prefer a 'delayed' instanciation of the machinery I need
to serve different requests.
For example: most of my PHP
requests will be to send out JSON back for my Ajax/dojo client.
And the data for the requests will be coming from more than one database,
memcache, and possibly another webserver (since I cannot do crossdomain
ajax calls while within https)

So I do not want to unnecessarely instanciate variables/classes/etc
all the time only if I need some functionality for a particular request.



b) I did not feel comfortable with model/view/controller to
'modularize my code' because I cannot decide early enough what
functions I will need for each controller and what data I will need for each.

And the M/V/C prohibits easy way to call functions of one class instance
from another (especially it prohibits making your classes to inherit
from PHP's Spl datastructure classes -- that I use to organize my iterative and recursive/logic)

Therefore right now I feel much more comfortable using CI's
'library' and 'helpers' to modularize my code -- because those are
very un-assuming about who will invoke them and why, and that's how
I like it.
#9

[eluser]sophistry[/eluser]
thank you, that was a very nice explanation. that makes it much clearer why you want to "subvert" a large part of CI architecture.

good luck in your endeavors. :-)




Theme © iAndrew 2016 - Forum software by © MyBB