Liglo App
Liglo App

Reputation: 3819

CakePHP 3 - Rename controller in routes.php

What would be the correct way to reroute all actions from fakeController to controller1 using Cake's routing engine?

I'd like to reroute actions index and any other actions and also parameters.

app/fake/ => app/controller1/

app/fake/action1 => app/controller1/action1

app/fake/action2/any/params => app/controller1/action2/any/params

Is it possible with just one line of code?

Why I am doing this? - because in CakePHP 3 the routes are case-sensitive. I'd like to keep my controllers names UpperCase, but it results in paths like app/Users/login, if I write users it says usersController not found. If there is a way around this, I wouldn't need all this rerouting.

Upvotes: 0

Views: 1036

Answers (1)

ndm
ndm

Reputation: 60463

The default route is case-sensitive, yes, however by default there should be fallbacks defined using the InflectedRoute class, which behave as known from 2.x, ie it will inflect users to Users (as of 3.1.0 the default is DashedRoute, which inflects too).

https://github.com/cakephp/app/blob/3.0.4/config/routes.php#L73

If you want this to be the default behavior (note that this is relatively slow) for all routes, then simply set this to be the default via Router::defaultRouteClass()

Router::defaultRouteClass('InflectedRoute');

https://github.com/cakephp/app/blob/3.0.4/config/routes.php#L42

or in order to restrict it to a specific scope, use the fallbacks() method.

$routes->fallbacks('InflectedRoute');

https://github.com/cakephp/app/blob/3.0.4/config/routes.php#L73

Alternatively you could create appropriate routes for all your controllers, similar to as shown in your apps routes.php file:

Router::scope('/', function (\Cake\Routing\RouteBuilder $routes) {
    // ...

    $routes->connect('/users', ['controller' => 'Users', 'action' => 'index']);
    $routes->connect('/users/:action/*', ['controller' => 'Users']);

    $routes->connect('/foos', ['controller' => 'Foos', 'action' => 'index']);
    $routes->connect('/foos/:action/*', ['controller' => 'Users']);

    // and so on...
});

https://github.com/cakephp/app/blob/3.0.4/config/routes.php#L60-L62

See Cookbook > Routing for more information about routing.

Upvotes: 1

Related Questions