Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bake admin (prefixed) code with CakePHP 3?

Can anyone tell me what is the official way to create CRUD for admin back-end?

In CakePHP 2 the baked code was extended with 'admin_' before the function names and for the view files. In CakePHP it seems I can't find any direct information on how it's done anymore. The bake console doesn't ask for admin anymore. In this topic: https://github.com/cakephp/bake/issues/28 I see that they mention to use the --prefix extension but then the controller is placed in a separate /Admin folder while the CRUD functions keep having their normal name. And in some parts of the cookbook () I still see they mention functions like admin_view.

So can anyone tell me what is the official 'Cake'-way to do this from 3.2 on?

like image 548
Lucky Avatar asked Dec 15 '22 04:12

Lucky


1 Answers

If you want to create Controller using cake bake. You can do this with below command:

bin/cake bake controller --prefix admin users

For view:

bin/cake bake template --prefix admin users

It creates the admin folder in the template directory, Then it creates the folder for users, then it includes the files. for admin prefix folder structure like template/admin/users/index.ctp

See official cookbook documentation

Also In your config/routes.php add this:

Router::prefix('admin', function ($routes) {
    $routes->connect('/', ['controller' => 'Users', 'action' => 'index']);
    $routes->extensions(['json', 'xml']);
    // All routes here will be prefixed with `/admin`
    // And have the prefix => admin route element added.
    $routes->fallbacks('DashedRoute');
});
like image 133
Faisal Avatar answered Jan 08 '23 06:01

Faisal