Welcome Guest, Not a member yet? Register   Sign In
method that loads before controller modules
#1

[eluser]pocketmax[/eluser]
I have a controller with a bunch of modules in it and I can reduce a lot of duplicate code if there was a central place that I could put a piece of code that would load before my modules do. Is there a way to put code in a method that would run before any of my modules run? So if someone goes to myurl.com/blog/update/

and I in my code...

Code:
class Blog extends Controller {

      function BeforeOtherModules(){
          $this->foo='bar';
      }

      function update(){
          echo $this->foo;
      }
}
#2

[eluser]WebsiteDuck[/eluser]
Code:
class Blog extends Controller {

      function __construct(){
          parent::Controller();
          $this->foo='bar';
      }

      function update(){
          echo $this->foo;
      }
}
#3

[eluser]pocketmax[/eluser]
What I meant was something CI specific. for example, cake has the _beforeScaffold method. But if using the construct is the only/best way of doing it, I'm down for it. Thanks
#4

[eluser]WebsiteDuck[/eluser]
Sorry I misunderstood, maybe hooks is what you're looking for?

http://ellislab.com/codeigniter/user-gui...hooks.html
#5

[eluser]pocketmax[/eluser]
Yeah, I read that and I'm a little confused. From a first glance, it looks really powerful but it's example doesn't make sense to me...

Code:
$hook['pre_controller'][] = array(
                                'class'    => 'MyClass',
                                'function' => 'Myfunction',
                                'filename' => 'Myclass.php',
                                'filepath' => 'hooks',
                                'params'   => array('beer', 'wine', 'snacks')
                                );

It doesn't say whether or not the classes and functions are custom, pre-exist have to be created or are already created. Heres a better example of what I'm talking about...

I have a controller named Blog with a module named update and I want a hook to fire before that controller runs, so I would do this?

Code:
$hook['pre_controller'][] = array(
                                'class'    => 'Blog',
                                'function' => 'update',
                                'filename' => 'pre_blog.php',
                                'filepath' => 'hooks',
                                'params'   => array('test', '1', '23')
                                );

So with that code, I'm telling it what controller, what module, where the file is that has the hook, the filepath to the hook and the default params I want to pass into the hook? I know they give a definition of what each entry means but it's a little confusing to me. Also, it doesn't give an example of the actual contents of the hook it's self. Is the hook a class and CI creates an object from it on the fly to glue it to my controller? is it an object I instantiate myself from a class I wrote or is it a simple function? And if it's a function, won't I loose scope since functions don't get $this? So won't that limit the abilities of my hook?
#6

[eluser]jedd[/eluser]
[quote author="pocketmax" date="1263267431"]
I have a controller with a bunch of modules in it and I can reduce a lot of duplicate code if there was a central place that I could put a piece of code that would load before my modules do.
[/quote]

This kind of thing is covered in [url="/wiki/FAQ"]the FAQ[/url] I think.
#7

[eluser]Dyllon[/eluser]
[quote author="pocketmax" date="1263267431"]I have a controller with a bunch of modules in it and I can reduce a lot of duplicate code if there was a central place that I could put a piece of code that would load before my modules do. Is there a way to put code in a method that would run before any of my modules run? So if someone goes to myurl.com/blog/update/

and I in my code...

Code:
class Blog extends Controller {

      function BeforeOtherModules(){
          $this->foo='bar';
      }

      function update(){
          echo $this->foo;
      }
}
[/quote]

I'm guessing by modules you mean methods in which case you can put $this->foo='bar'; in the constructor
#8

[eluser]pocketmax[/eluser]
I thought when classes are used as controllers, the methods are suppose to be called modules? If not, correct me now so I get it right later.
#9

[eluser]Dyllon[/eluser]
Modules are a group of components (models, controllers, views, etc.) and are not natively supported by CI.

The functions in a controller are referred to as methods
#10

[eluser]laytone[/eluser]
A module is a part of a component which is a part of a system and really the definition of a module has nothing to do with anything.

In joomla a module is one part of a page.

Define Module:
# a self-contained component (unit or item) that is used in combination with other components
or
# An independent part of a program. Much of Apache's functionality is contained in modules that you can choose to include or exclude. ...

Definitely not another way to say 'method' or 'function'!

Use your constructor and / or extend your controller class to solve your problem.. Constructors a specifically used to call function (aka methods) before any other function in the class are called. use constructors to load common variables and manipulate data that need to be access-able from any part of your class / controller.




Theme © iAndrew 2016 - Forum software by © MyBB