0

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 ?

John
  • 4,711
  • 9
  • 51
  • 101

2 Answers2

3

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);
Timothy
  • 2,004
  • 3
  • 23
  • 29
  • thanks for your answer. What is theses parameters 1, 2, 3 for the controller action etc. I don't understand – John Apr 08 '16 at 23:10
  • 1
    Check https://docs.phalconphp.com/en/latest/reference/routing.html - for more details about it... but it is something like a map of route- u define which part - how to name it. For example, 1 is the controller and will be get from first part "/([a-zA-Z\-]+)/". There is a few ways to describe your routes. – Boris Delev Apr 09 '16 at 09:20
  • check out the link @BorisDelev provided you, it explains how the Phalcon router translates a given URL – Timothy Apr 09 '16 at 11:15
  • Thanks a lot. This is awesome. – John Apr 10 '16 at 00:04
1

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'
       ]);
Timothy
  • 2,004
  • 3
  • 23
  • 29
Nikolay Mihaylov
  • 3,868
  • 8
  • 27
  • 32