Welcome Guest, Not a member yet? Register   Sign In
is hooks working with all the controllers?
#1

[eluser]young[/eluser]
I find that if I use hooks, all the controllers will be affected.
If I want that the hooks only affected some of the controllers, what can i do?

Besides, is it only nessary to edit the config file and edit the class file of the hooks, and no need to add post_controller_constructor function to the controller files?
#2

[eluser]Phil Sturgeon[/eluser]
If you are using pre_controller, use the following code to access the URI class:

Code:
function Myfunction($controllers) {

$uri =& load_class('URI');
if(!in_array($uri->segment(1, ''), $controllers)
{
   // Return out of the function and don't run the code
   return;
}

// Whatever your hook does here.
// ....

}


Then in your config file, pass it an array of controllers to run for:

Code:
$hook['pre_controller'] = array(
    'function' => 'Myfunction',
    'filename' => 'Myfunction.php',
    'filepath' => 'hooks',
    'params'   => array('controller1', 'controller2', 'other_allowed_controller')
);
#3

[eluser]young[/eluser]
thanks, so it means it'll affect all the controllers, and you have to check it whether to use hook or not in the hook function
#4

[eluser]Phil Sturgeon[/eluser]
Normally a hook affects everything yes. This code will let you limit it to certain controllers, or by removing the ! in the if you could let if affect all controllers EXCEPT for the ones listed in the config.
#5

[eluser]young[/eluser]
really thank you very much, you helped me a lot
#6

[eluser]ELRafael[/eluser]
Code:
$controller = $this->CI->uri->segment(2);
$method = $this->CI->uri->segment(1);
if ($method === 'backoffice')
  //....

So, if the controller is backoffice (the management system of website), do something.
#7

[eluser]young[/eluser]
[quote author="ELRafael" date="1232147369"]
Code:
$controller = $this->CI->uri->segment(2);
$method = $this->CI->uri->segment(1);
if ($method === 'backoffice')
  //....

So, if the controller is backoffice (the management system of website), do something.[/quote]

thanks for your apply, i think it should be used after "post_controller_constructor", because CI hadn't instantiated before that.
then if i want to use hook in pre_controller, how can i load helper functions?
for example, i want to redirect the url in pre_controller
#8

[eluser]ELRafael[/eluser]
Oh, I forget it.

I use hook action in post_controller_constructor

So, I just call redirect(....); After constructor method of controller was called, the helps already loaded
#9

[eluser]Phil Sturgeon[/eluser]
[quote author="young" date="1233909426"]
So, if the controller is backoffice (the management system of website), do something.[/quote]

thanks for your apply, i think it should be used after "post_controller_constructor", because CI hadn't instantiated before that.
then if i want to use hook in pre_controller, how can i load helper functions?
for example, i want to redirect the url in pre_controller[/quote]

You need to remember that anything you do in pre_controller is going to be a hack. Only certain parts of the CI framework are loaded, and this instance itself has not really got going.

It is possible to include a helper simply by using include_once(APPPATH.'helpers/text_helper.'.EXT); but other than that you don't have any access to load them.

The problem is that any model, library or helper that tires to access the instance before it is created, will screw up. url_helper is one of these, as most of the functions call site_url() which calls the instance to get the config class.

You would need to make a MY_url_helper.php and override site_url() with this function.

Code:
if ( ! function_exists('site_url'))
{
    function site_url($uri = '')
    {
        $config =& load_class('Config');
        return $config->site_url($uri);
    }
}

That will stop it from accessing the entire instance, so you can use the url helper to get redirect.

That's just a theory. Let me know if you would like a few more examples of code, will knock something together and test it.

Would do it now, but just moved my VPS and I have 7 broken live sites right now...!
#10

[eluser]young[/eluser]
Thanks, it works now.
I think I should read the source code carefully to comprehend CI.




Theme © iAndrew 2016 - Forum software by © MyBB