Abhishek Abhishek - 4 months ago 27
PHP Question

CakePHP: get current model name in a controller

I'm creating a behaviour that needs to log the current model name. How can I get the current model name from within a controller in CakePHP?

Answer

Your architecture is flawed when you want to get a controllers primary model name from a behavior. That's a violation of the MVC pattern. Your app should have fat models and skinny controllers.

Your Behavior already has the model instance as it is required to be passed to behaviors. You can get the name from the model object:

// Cake 2.x
$Model->name
// Cake 3.x
$Table->name()

or it's alias

// Cake 2.x
$Model->alias
// Cake 3.x
$Table->alias()
Comments