Reputation: 328
Anybody can suggest me that how to get controller name in cakephp component function?
Upvotes: 0
Views: 1814
Reputation: 4765
try with:
$ctrObj = $this->_Collection->getController();
$ctrName = get_class($ctrObj);
die('<h1>Class name: ' . $ctrName . '</h1>');
// Result: "Class name: FooController"
$ctrName = $ctrObj->name;
die('<h1>Simple Name: ' . $ctrName . '</h1>');
// Result: "Simple Name: Foo"
Good luck
Upvotes: 1
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
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