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.
Hello Friends, I’m Arvind Kumar, founder of Inimist Technologies, Inimist Academy, The Fundabook and many more. I am a Full Stack Web Developer and Programmer by heart, and a Hindi/English blogger by chance. I build Web and Mobile Application around React, VueJS, NodeJS and PHP frameworks.
Have a project in mind? Get in touch