Welcome Guest, Not a member yet? Register   Sign In
Should I make a library, module or singleton class...
#1

[eluser]uprise78[/eluser]
Hi,

I am new to CI and had a quick question. It is probably blatantly obvious, but I havent seen an answer on the forums on in the docs. I need to be able to call a method on every page of my app that will parse a cookie and pre-fill some variables with the contents. Should I put this in a library and add it to the config or use a singleton class that gets instantiated in the constructor of my model or some other way?

Thanks in advance,
Mike
#2

[eluser]Clooner[/eluser]
There are several solutions for this. If it is only one method the quickest way is to use a helper and autoload this. For several I would put in a library and autoload this or you could even extend the controller and put it in there...

It is just what you prefer.

I guess the quickest way is the helper way.
#3

[eluser]webthink[/eluser]
I believe the original poster wanted to execute the code on every page which is not going to happen simply by putting it in a helper and autoloading it.
If you want the "CI Way" to do what your suggesting have a look at Hooks
#4

[eluser]Rick Jolly[/eluser]
[quote author="webthink" date="1205845069"]I believe the original poster wanted to execute the code on every page which is not going to happen simply by putting it in a helper and autoloading it.
[/quote]
True, but aren't autoloaded libraries included and instantiated. In that case, any code called from the constructor of an autoloaded library will be executed.
[quote author="webthink" date="1205845069"]
If you want the "CI Way" to do what your suggesting have a look at Hooks[/quote]
The problem with hooks and autoloading is they lack transparency. Without code comments, anyone maintaining the code doesn't necessarily know code is being included with those methods. It's better practice to extend the controller and call the code in the constructor of the parent controller. Kohana takes this approach.
#5

[eluser]uprise78[/eluser]
I took a look at Kohana and I like it a lot. I much prefer the coding conventions that strictly using PHP 5 brings but my issue was that it was quite a bit slower than CI in my benchmarks. With a near identical setup between the two Kohana was usually on the order of 10 - 40% slower to render than CI so I decided to go with CI. Kohana had a nice feature that allowed you to have more than one controller.

I am tempted to start pulling out all the conditionals for php4 from CI and patch up the inheritance tree to use PHP 5 features a bit more fluidly but I fear doing that will leave an ugly upgrade path.

Performance is paramount for the application that I am making which is why I ended up using CI over Kohana. Perhaps fixing up the Base4/Base5 and Controller.php muck would allow easier integration of globally available functions by subclassing CI_Controller? Or perhaps just adding a few methods to Controller.php?

What do the more experienced CI users think?

EDIT: This ones for all the Kohana fans out there so as not to start any misinformation going around. Note that this is all from my limited benchmarking and is in no way conclusive. CI is significantly faster when there are no database hits involved. Kohana is a bit faster when hitting SQL for some data. So, basically all that means is that Kohana's DB interface is a bit quicker than CI's.
#6

[eluser]Pygon[/eluser]
You could also just extend the Controller and put your functions there, making them available across the entire website. You can even have them run automatically as part of your constructor ala:

Code:
class MY_Controller extends Controller {

  function MY_Controller()
  {
    if(!$_SESSION['init_done'])
    {
      $this->doJunk();
      $_SESSION['init_done'] = TRUE;
    }
    
    parent::Controller();
  }

  function doJunk()
  {
     // Do some junk.
  }
}

Then just have your controllers extend MY_Controller.
#7

[eluser]webthink[/eluser]
@Rick Jolly

Yeah I agree. I'm not a huge fan of Hooks for that very reason. That's why I said the "CI Way" in quotes. I thought I'd present the "Official" way of auto-executing code and let the OP research from there. We often miss the forest for the trees on this forum offering "best practice" solutions before we offer the obvious ones. Are helper classes instantiated though? I can't remember. I know libraries are.

Either way I was responding more to clooner's suggestion which, while I understand the implications, didn't suggest anything to do with a constructor to the OP so was kind of misleading. It gave the impression that simply autoloading the helper is all that's necessary.

I have to say it does seem odd and not at all transparent to have a helper/library class who's sole purpose is to run a constructor with possibly no other code/methods.

Pygon's make-a-base-controller-and-then-extend-it suggestion is fast becoming a popular opinion around here and and may well be the best way to accomplish this. I'm still undecided. I'd personally like to see hooks reimplemented.
#8

[eluser]esra[/eluser]
Another alternative is the Khaos Event Manager library. I have not tried this myself, but it is supposed to be similar to the plugin system used in Joomla which works really nice. Correct me if I'm wrong, but I believe you should be able to trigger an event from any controller within one or more methods, Event Manager listens for the event, then Event Manager executes code in a plugin associated with the event. There is very little documentation for this library, but the Joomla developer docs should give you a good idea of the potential of this library.

I use the Base Controller approach myself. In some base controllers, I add helper code after the closing brace for the base controller class when the helper functions are required by all controllers. However, the helper functions could just as well been loaded from a separate helper file.
#9

[eluser]the_fury[/eluser]
Extend the controller, absolutely. That's what inheritance is there for.




Theme © iAndrew 2016 - Forum software by © MyBB