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

#1
[eluser]zackwragg[/eluser]
Hi,

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:

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

However, if the user is trying to load http://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:

Code:
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.

#2
[eluser]zackwragg[/eluser]
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:

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

#3
[eluser]Phil Sturgeon[/eluser]
Code:
if( !$resource = $this->CI->uri->segment(1, '') )
{
    $this->CI->load->helper('url');
    redirect('default_resource');
}

#4
[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  


Users browsing this thread:
1 Guest(s)


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