pavkatar
pavkatar

Reputation: 53

ZF2 dynamic routing

I have problem with the routing in ZF2. I want to make dynamic routing for the software, that I'm making.

For example: This is the URL: http://localhost:8080/application/index.json/ And this is my module.config (router part):

'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Segment',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                    ),
                ),
            ),
            'restful' => array(
                'type'    => 'Zend\Mvc\Router\Http\Segment',
                'options' => array(
                    'route'       => '/:module/[:controller[/:action][.:formatter][/:id]]',
                    'constraints' => array(

                        'module' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'formatter'  => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'         => '[a-zA-Z0-9_-]*'
                    ),


                ),
            ), 

      ), 
 ), 

Everything is working fine, but when I create new controller, have to add it to controllers['invokables'] setting in module.config.

'controllers' => array(
    'invokables' => array(
        'index' => 'Application\Controller\IndexController',
        'cloud' => 'Application\Controller\CloudController',
    ),
),

So the question is, how to automate the controllers['invokables'] to process requests dynamically, without describing every controller in it.

Upvotes: 0

Views: 1724

Answers (1)

akond
akond

Reputation: 16035

Fast and dirty, but you get the idea.

namespace Application;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager        = $e->getApplication()->getEventManager();

        $eventManager->attach (MvcEvent::EVENT_ROUTE, function (MvcEvent $e) {
            $controller_loader = $e->getApplication ()->getServiceManager ()->get ('ControllerLoader');

            $controller = $e->getRouteMatch ()->getParam ('controller');
            $controller_class = '\Application\Controller\\'.ucfirst ($controller).'Controller';

            // Add service locator to the controller
            $controller_object = new $controller_class;
            $controller_object->setServiceLocator ($e->getApplication ()->getServiceManager ());
            // ------------------------------------
            $controller_loader->setService ($controller, $controller_object);
        });
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
}

Upvotes: 1

Related Questions