Rashid Ahmad Khan
Rashid Ahmad Khan

Reputation: 328

How to get controller name in cakephp (2.x) component?

Anybody can suggest me that how to get controller name in cakephp component function?

Upvotes: 0

Views: 1814

Answers (5)

fitorec
fitorec

Reputation: 4765

try with:

 $ctrObj = $this->_Collection->getController();

Getting class name:

 $ctrName = get_class($ctrObj);
 die('<h1>Class name: ' . $ctrName . '</h1>');
 // Result: "Class name: FooController"

Getting simple name:

 $ctrName = $ctrObj->name;
 die('<h1>Simple Name: ' . $ctrName . '</h1>');
 // Result: "Simple Name: Foo"

Good luck

Upvotes: 1

Hai Nguyen
Hai Nguyen

Reputation: 927

try this

Router::getRequest(true)->param('controller');

Upvotes: 1

gounane
gounane

Reputation: 381

Use $this->params['controller'] to get the current controller.

You can do a debug($this->params) to see other available variables.

Upvotes: 0

Mumtaz Ahmad
Mumtaz Ahmad

Reputation: 432

$this->request->params['controller']

Upvotes: 0

marmeladze
marmeladze

Reputation: 6564

I'm not familiar with CakePHP very much, and possibly there is a built-in method for this. But if you are running PHP 5.5+, below code might give you inspiration

class Foo {
  public function name_of_this_controller() {
    return static::class;
  }
}

$f = new Foo();
echo $f->name_of_this_controller() #=> "Foo"

for older versions of php,

get_class($f) 

will work.

So, inside components (I am referring here) assigning static::class to a variable will do work, I believe.

Upvotes: 0

Related Questions