cakephpurl-routingcakephp-3.3cakephp-routing

routing in CakePHP 3


I'm working on CakePHP 3.3

I have some dashboard controllers which are named like

DashboardUsersController.php, 
DashboardBusinessesController.php,
DashboardCustomersController.php, 
etc

I want to map urls like

http://example.com/dashboard/users/view/param1/param2

And this will call DashboardUsersController.php class and view function with param1 and param2 as parameters.

In short I want to change url http://example.com/dashboard-users/view/param to http://example.com/dashboard/users/view/param

this type of mapping will be done only if dashboard is present after domain, otherwise it will work as default like on accessing http://example.com/users/view/param1 will call UsersController.php

What I have done till now?

Since, I'm new to CakePHP and routing, I have no idea where to start from and therefore have done nothing till now. I need your help.


Solution

  • I think what you needed is prefix. Bake your controller model with prefix dashboard .

    Use this in you routes.php

    use Cake\Routing\Route\DashedRoute;
    
    Router::prefix('dashboard', function ($routes) {
        // All routes here will be prefixed with `/dashboard
        $routes->fallbacks(DashedRoute::class);
    });
    

    And remove that dashboard part from controllers or remove dashboard from your table name and rebake everything with --prefix .

    bin/cake bake all --prefix dashboard

    These links will help you

    https://book.cakephp.org/3.0/en/development/routing.html#prefix-routing

    https://book.cakephp.org/3.0/en/bake/usage.html