0

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.

Gaurav
  • 131
  • 12

1 Answers1

4

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

Aman Rawat
  • 2,625
  • 1
  • 25
  • 40
newbie
  • 195
  • 1
  • 10
  • 1
    Am glad to help yu :) – newbie Feb 02 '17 at 04:59
  • There is an issue. I have two `UsersController.php` classes each in `/src/controller/Dashboard/` prefix and in `/src/controller/`. I'm using `/src/controller/UsersController.php` to login in `authComponent` but on trying to access unautherised page in `dashboard` prefix. It is redirecting to `/src/controller/dashboard/users/login` instead of `/src/controller/users/login` – Gaurav Feb 02 '17 at 08:54
  • got it working... added `'prefix' => false` in `authComponent` – Gaurav Feb 02 '17 at 09:02
  • Was about to comment d same – newbie Feb 02 '17 at 09:11