Phalcon多模块路由其他模块

时间:2016-04-08 15:03:58

标签: php routing phalcon multi-module

我正在使用Phalcon php。我必须尝试使用​​multi modules architecture。我有一个前端和后端。前端应用程序是默认模块。但我不了解其他模块。如果后端有50个控制器,控制器有10个动作,我必须为后端模块定义所有路由吗?

2 个答案:

答案 0 :(得分:3)

对于您的后端路线,您不必定义50多条不同的路线以匹配您的所有控制器/动作组合。你可以坚持使用Phalcon提供的默认路线。

这是一个可能符合您需求的示例。我不确定你的确切项目结构是什么。但是从你提供的例子开始,试试这个:

$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);

答案 1 :(得分:1)

我和你一样使用相同的场景。无需定义所有可能的路线。以下是我的路线,它们适用于CMS领域我需要的任何东西:

// 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'
       ]);