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

3

5

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?

Amalekite answered 31/7, 2016 at 8:58 Comment(1)
And in some parts of the cookbook: book.cakephp.org/3.0/en/views.html#layoutsAmalekite
S
10

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');
});
Samuele answered 1/5, 2017 at 9:56 Comment(0)
L
1

That's how things work now in CakePHP 3, prefixed methods are gone, prefixes now do generate separate controllers in sub-namespaces, for smaller/simpler controllers, and for proper separation, not only on controller level, but also on template level, where the templates are expected to be placed in separate folders accordingly.

The admin_view example you are referring to is just an example that should show how to manually set a custom layout for specific actions, it has nothing to do with prefix routing.

So, if you want to use prefix routing, then the "official" way is to use the --prefix option.

See also

Leatherneck answered 31/7, 2016 at 12:40 Comment(3)
Thanks. I understand. However I have a problem with the bake templates used for the admin. I have put a controller bake file in src/Template/Bake/Controller/Admin/controller.ctp but baking a controller with --prefix admin doesn't use that file. Can you tell me what I'm doing wrong?Amalekite
Can anyone tell me where/how to save the new bake template for prefix routing? When I bake a controller with --prefix Admin it creates the Controller/Admin/controller.php file from the Template/Bake/Controller/controller.php file instead of from the Template/Bake/Controller/Admin/controller.php file. But I want different code generated in the main controller and in the Admin/controller.Amalekite
@Amalekite Please open a new question.Leatherneck
U
1

Below is the bake command to bake all prefix controller and templates for users table

cake bake all users --prefix admin

And here is the route code to make it working:-

Router::prefix('admin', function ($routes) {
        // Because you are in the admin scope,
        // you do not need to include the /admin prefix
        // or the admin route element.
        $routes->connect('/', ['controller' => 'Users', 'action' => 'index']);
        $routes->extensions(['json', 'xml']);
        // All routes here will be prefixed with `/admin`
    //$routes->connect('/admin', ['controller' => 'Order', 'action' => 'index']); // call other controller like this
        // And have the prefix => admin route element added.
        $routes->fallbacks('DashedRoute');
    }); 

This will work for me hope it will work for you :)

Uncommon answered 5/8, 2017 at 6:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.