How to get model name from controller in cakephp

Suppose we have $controller as an object for our controller class. We can do the following to get the model class:

[code]$modelName = $controller->modelClass;[/code]

To get the most obvious model name based on the the controller name, the following line of code would do:

$modelName = Inflector::camelize(Inflector::singularize($controller->params['controller']));

On the other hand, you can try the following example to get a controller name from model name in CakePHP. Of course, this only works to get the most obvious and conventional name, as per naming conventions in Cakephp. Here is the example:

$controllerName = Inflector::pluralize(Inflector::underscore($modelName));

Let me know if you know of a better way to do the same.

Leave a Reply