Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

I'm looking for the briefest way to setup a prefix such as /admin/ to use the default routing style for a module

I'm looking for a solution to add /admin to use the admin module to use all of the default routes.

I suppose that I could use something like the following regex but I'm not sure if its the correct way. I'm open to using a router group.

My current method:

$router->add('/admin/:controller', [
    'module' => 'admin',
    'controller' => 1
]);
$router->add('/admin/:controller/:action', [
    'module' => 'admin',
    'controller' => 1,
    'action' => 2,
]);
$router->add('/admin/:controller/:action/:params', [
    'module' => 'admin',
    'controller' => 1,
    'action' => 2,
    'params' => 3
]);


7.4k
Accepted
answer

I think this is the best solution (I've tried many other solutions before):

<?php

/**
 * Routes
 * @author Aboozar Ghafari <[email protected]>
 */

namespace Multiple\Admin;

class Routes
{

    public function add($router)
    {
        $router->add("/admin", array(
            'module' => 'admin',
            'controller' => 'index',
            'action' => 'index'
        ));
        $router->add('/admin/:controller', array(
            'module' => 'admin',
            'controller' => 1,
            'action' => 'index',
        ));

        $router->add('/admin/:controller/:action', array(
            'module' => 'admin',
            'controller' => 1,
            'action' => 2,
        ));
        $router->add(
            "/admin/:controller/:action/:params",
            array(
                'module' => 'admin',
                "controller" => 1,
                "action"     => 2,
                "params"     => 3,
            )
        );
        return $router;
    }
}