Conny Olsson
Conny Olsson

Reputation: 1577

codeigniter get current controller in post controller constructor hook

I'm trying to get the name of the called controller in a "post controller constructor" hook:

<?php
function authenticate() {
    $CI =& get_instance();
    $controller = $CI->router->class;
}

But I get this error message:

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: hooks/post_controller_constructor.php

Line Number: 5

Backtrace:

File: / ... /CodeIgniter-3.0rc3/application/hooks/post_controller_constructor.php Line: 5 Function: _error_handler

File: / ... /index.php Line: 292 Function: require_once

Any ideas why?

Upvotes: 1

Views: 3484

Answers (2)

swap
swap

Reputation: 282

<?php
      function authenticate() 
      {
         $CI = &get_instance();
         $controller = $CI->router->fetch_class();  //Controller name
         $method     = $CI->router->fetch_method();  //Method name
      }

Upvotes: 2

Tariq Husain
Tariq Husain

Reputation: 566

i am not sure about this. but you can try like this. fetch_class is used to fetch the class name and method is used to fetch the method name.

$this->router->fetch_class(); 
$this->router->fetch_method();

Upvotes: 0

Related Questions