• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing controller and function name from hook


I have just got Zend_ACL working and hooked to the front controller of my application. What I would like to do is have it check which controller and function a user is trying to load and decide if they have access. If they do, then load the controller. If not, redirect them to an error page. In this scenario, the controller would be my ACL resource and the function would the ACL privilege.

Now, my question is, is there an easy way to access the controller name and function name of the controller and function that the front controller is trying to load. At the moment I have doing this:

$resource = $this->CI->uri->segment(1);
$privilege = $this->CI->uri->segment(2);

However, if the user is trying to load www.example.com/ (i.e. with no query string) then this wouldn't work. I could easily put something like this in here to combat this problem:

if( $resource === null )
    $resource = 'default_resource';

I was just wondering if there is a specific way to access those names from the CI instance that would be a bit nicer.

In case it is relevant, I have my hook on the post_controller_constructor hook.

Any help is greatly appreciated.

Actually, with a little bit of following the code through and delving into the CI files, I found the answer.

If anyone was interested in how you access these, you can use the router functions below:

$resource = $this->CI->router->fetch_class();
$privilege = $this->CI->router->fetch_method();

[eluser]Phil Sturgeon[/eluser]
if( !$resource = $this->CI->uri->segment(1, '') )

[eluser]Phil Sturgeon[/eluser]
Had some trouble using those with Matchbox, hence my post using URI segments. Might be an idea to use uri->rsegment() too, so you get the fully routed URL segments and not what happens to be in the URI.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.