Phalcon Multi-Module Routing

橙三吉。 提交于 2019-12-04 16:09:06

I have my general routes defined in app\config\routes.php then in app\config\routes I have one file for each of the groups (in this case modules).

app\config\routes.php

<?php
$router = new \Phalcon\Mvc\Router();

$router->setDefaultModule("frontend");

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

foreach (glob(__DIR__ . "/routes/*.php") as $filename) {
    include $filename;
}

$router->removeExtraSlashes(true);

return $router;

admin\config\routes\admin.php

<?php

use Phalcon\Mvc\Router\Group;

//Create a group with a common module and controller
$admin = new Group(array('module' => 'admin'));

$admin->setPrefix('/admin');

$admin->add("", array(
    'controller' => 'index',
    'action' => 'index'
));

$admin->add("/foo", array(
    'controller' => 'foo',
    'action' => 'index'
));

$admin->add("/bar", array(
    'controller' => 'bar',
    'action' => 'index'
));

$router->mount($admin);

I have only had to define the controller and index because any other actions will be part of the url and should route by themselves.

EDIT

index.php

/*
 * ... includes
 */

$application = new \Phalcon\Mvc\Application($di);

$application->registerModules(array(
    'admin' => array(
        'className' => 'Calmsplash\App\Admin',
        'path' => '../app/admin/Admin.php',
    ),
    'frontend' => array(
        'className' => 'Calmsplash\App\Frontend',
        'path' => '../app/frontend/Frontend.php',
    ),
    /// ... more modules
));

I've called my Module files after the module name just to fit them into the namespace.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!