PHP Phalcon Wild card routing in multi module application

1.5k views Asked by At

UPDATE This is now fixed with this solution.

    foreach (array('core', 'backend' => array('alias' => 'admin'), 'api') as $module => $options) {

            // If an alias is set use that
            if (isset($options['alias']) && !empty($options['alias'])) {
                $module = $options['alias'];
            }

            // If module is an int then $options contains the module name
            if (is_int($module)) {
                $module = $options;
            }

            $group = new \Phalcon\Mvc\Router\Group(array(
                'module' => $module,
            ));

            $group->setPrefix('/' . (isset($options['alias']) ? $options['alias'] : $module));

            // Allow camel case controller and action name that will be accessed via dashes
            $group->add('/([a-zA-Z\-]+)/([a-zA-Z\-]+)/:params', array(
                'controller' => 1,
                'action' => 2,
                'params' => 3
            ))->convert('action', function($action) {
                return \Phalcon\Text::lower(\Phalcon\Text::camelize($action));
            });

            // Mount a group of routes for some module
            $router->mount($group);
        }

Here is my router in my app/Bootstrap.php file

protected function router()
{

    $this->di->set('router', function() {
        $router = new Router(false);

        $router->setDefaults(array(
            'module' => $this->config->router->default->module,
            'controller' => $this->config->router->default->controller,
            'action' => $this->config->router->default->action
        ));

        /*
         * All defined routes are traversed in reverse order until Phalcon\Mvc\Router
         * finds the one that matches the given URI and processes it, while ignoring the rest.
         */
        $frontend = new \Phalcon\Mvc\Router\Group(array(
            'module' => 'frontend',
        ));

        // Allow camel case controller and action name that will be accessed via dashes
        $frontend->add('/([a-zA-Z\-]+)/([a-zA-Z\-]+)/:params', array(
            'controller' => 1,
            'action' => 2,
            'params' => 3
        ))->convert('action', function($action) {
            return \Phalcon\Text::lower(\Phalcon\Text::camelize($action));
        });

        // Mount a group of routes for frontend
        $router->mount($frontend);

        /**
         * Define routes for each module
         */
        //foreach ($this->getModules() as $module => $options) {
        foreach (array('core', 'backend' => array('alias' => 'admin'), 'api') as $module => $options) {
            $group = new \Phalcon\Mvc\Router\Group(array(
                'module' => $module,
            ));

            $group->setPrefix('/' . (isset($options['alias']) ? $options['alias'] : $module));

            // Allow camel case controller and action name that will be accessed via dashes
            $group->add('/([a-zA-Z\-]+)/([a-zA-Z\-]+)/:params', array(
                'controller' => 1,
                'action' => 2,
                'params' => 3
            ))->convert('action', function($action) {
                return \Phalcon\Text::lower(\Phalcon\Text::camelize($action));
            });

            // Mount a group of routes for some module
            $router->mount($group);
        }

        return $router;
    });
}

Here is my directory structure: http://pastie.org/9709545

My issues is when I go to http://example.com/api/sms/send it gives me a 404 page (hits frontend/Module.php when it should be hitting api/v1.0.x/Module.php)

When I do have a SmsController -> send action?

Anyone know how to route multi modules correctly?

Thanks

0

There are 0 answers