John
John

Reputation: 4991

Phalcon multi module routes for other modules

I'm using Phalcon php. I have to try to use the multi modules architecture. I have a frontend and backend. The frontend app is the default module. But I don't understand something about the other modules. If I have 50 controllers in the backend with 10 actions by controllers I have to define all routes for the backend module ?

Upvotes: 0

Views: 1010

Answers (2)

Timothy
Timothy

Reputation: 2003

For your backend routes you don't have to define 50+ different routes to match all your controller / action combinations. You can mostly stick with the default routes Phalcon provides.

This is an example that might fit your needs. I am not sure what your exact project structure is. But going from the example you provided, try this:

$router = new Phalcon\Mvc\Router();

// set the defaults, so Phalcon knows where to start and where to fall back to
$router->setDefaultModule('frontend');
$router->setDefaultNamespace('Apps\Frontend\Controllers');
$router->setDefaultAction("index");
$router->setDefaultController("index");

$router->removeExtraSlashes(true);

/* ----------------------------------------------------- */
/* ------------------ FRONTEND ROUTES ------------------ */
/* ----------------------------------------------------- */

$router->add('/([a-zA-Z\-]+)/([a-zA-Z\-]+)/:params', [
    'module'     => 'frontend',
    'namespace'  => 'Apps\Frontend\Controllers',
    'controller' => 1,
    'action'     => 2,
    'params'     => 3
]);


/* ----------------------------------------------------- */
/* ------------------ BACKEND ROUTES ------------------- */
/* ----------------------------------------------------- */
// to keep your routes.php file clean,
// you can create a separate router group for your backend routes.

$backend = new Phalcon\Mvc\Router\Group();
$backend->setPrefix('/backend');

// for a backend route with a controller
$backend->add('/([a-zA-Z\-]+)', [
    'module'     => 'backend',
    'namespace'  => 'Apps\Backend\Controllers',
    'controller' => 1,
    'action'     => 'index'
]);

// for a backend route with a controller/action
$backend->add('/([a-zA-Z\-]+)/([a-zA-Z\-]+)', [
    'module'     => 'backend',
    'namespace'  => 'Apps\Backend\Controllers',
    'controller' => 1,
    'action'     => 2
]);

// for a backend route with a controller/action/parameter
$backend->add('/([a-zA-Z\-]+)/([a-zA-Z\-]+)/:params', [
    'module'     => 'backend',
    'namespace'  => 'Apps\Backend\Controllers',
    'controller' => 1,
    'action'     => 2,
    'params'     => 3
]);

// add your backend routes to the main router.
$router->mount($backend);

Upvotes: 3

Nikolay Mihaylov
Nikolay Mihaylov

Reputation: 3884

I'm using same scenario as you. There is no need to define all possible routes. Here are my routes and they are universal for anything i need in the CMS area:

// Frontend routes
// ....

// CMS Routes
$router->add('/cms', [
         'module' => 'backend', 
         'controller' => 'admin', 
         'action' => 'login'
        ]);

$router->add('/cms/:controller/:action/([0-9]+)/:params', [
         'module' => 'backend',
         'controller' => 1,
         'action' => 2,
         'id' => 3, 
         'params' => 4
        ])->setName('backend-full');

$router->add('/cms/:controller/:action', [
         'module' => 'backend',
         'controller' => 1,
         'action' => 2
        ])->setName('backend-short');

$router->add('/cms/:controller', [
         'module' => 'backend',
         'controller' => 1,
         'action' => 'index'
       ]);

Upvotes: 1

Related Questions