0
votes

I'm trying to set up prefix routing but only on certain controllers where I would want a separate admin version but I only seem to be able to make it work for all or nothing.

$routes->prefix('admin', function ($routes) {
    $routes->connect('/:controller/:action/*', [], ['routeClass' => 'Cake\Routing\Route\InflectedRoute']);
});

The above creates an admin prefix on every controller such that when i am within /admin/ all links to any controller created with the HTML helper have the admin prefix added to them and even when I'm redirected to login it is prefixed with /admin/. I would need to create admin version of every controller or specify on every link prefix=>false.

I assumed it would be something like:

$routes->prefix('admin', function ($routes) {
    $routes->connect('/projects/:action/*', ['controller' => 'Projects'], ['routeClass' => 'Cake\Routing\Route\InflectedRoute']);
});

but with this code when accessing a page within /admin/projects i get the error: Error: A route matching "array ( 'controller' => 'Users', 'action' => 'login', 'prefix' => 'admin', 'plugin' => NULL, '_ext' => NULL, )" could not be found. As if it is trying to redirect me to login, even though if i revert the code back the page loads and I have not been logged out.

Thanks in advance for your help.

1

1 Answers

0
votes

From talking to a colleague it turns out I'm slightly misunderstanding how prefix routing works. It's a false concept to connect certain controllers/routes to a prefix. All controllers can be accessed using the prefix and I just need to use the links within my app to ensure the user goes to the appropriate place - ie specify 'prefix'=>'false' if I want to leave the 'admin' prefix.

Cake will automatically merge the current prefix into all links if this is not specified.

With regards to being directed to an admin version of the login page. I just created an admin login method that redirects to the non-admin version.