How to use one Model in two controller

Dear Friends,

I am using CakePHP friendsofcake's crud, I have two controllers UserController.php in /Admin and VendorsController.php in /Vendors. but these Both Controllers should use single Model CustomersTable.php.

Please let me know how should I achieve this?

Cake’s MVC setup exists for a reason. Each table has its own controller. Using relations, you can make sure they are linked to eachother and queries can look up related data.
All controllers should be in folder Controller. Otherwise, Cake will not work.
So you will need CustomerController as well. Note that another controller can still look up data directly in other tables, using

use Cake\ORM\TableRegistry;


$SomeTable = TableRegistry::get(‘SomeTable’);

Use $this->loadModel(‘Some Table’) instead of TableRegistry. TableRegistry is an evil.

Can you please elaborate on “TableRegistry is an evil”?

TableRegistry is (was) just a static wrapper for TableLocator , that has been removed in 3.6.

You should not use any of static calls you can find in cake’s code or book as at some point they will just vanish leaving you with code that needs to be refactored before you will be able to update framework to recent version.

1 Like

If you are using Crud, then set the $modelClass property in the controller.


class VendorController extends AppController
    public $modelClass = 'Users';
1 Like