A
A
Aizharik2015-07-02 21:26:40
PHP
Aizharik, 2015-07-02 21:26:40

How to solve the problem Phalcon Router Service router cannot be resolved, with a multi-module project structure?

Hey!
I wanted to write a modular site on Phalcon.
Sketched a frame, everything works except Router.
Here is a structure


  • app
    • Cache
    • config
    • Core
    • Modules
      • index
        • Controllers
        • Models
        • Views
        • module.php
        • Routes.php

    • plugins
    • Views
    • Boostrap.php

  • public
    • index.php


Each module has Routes.php in its "init" method is passed as a parameter an instance of the BaseRoute object which is inherited from Phalcon\Mvc\Router .
At first, it threw an error only when I tried to add a group of routes, now it throws an error every time if there is a Routes.php file in the module folder.
Here is the text of the error:
Fatal error:  Uncaught exception 'Phalcon\Di\Exception' with message 'Service 'router' cannot be resolved' in C:\OpenServer\domains\Vtransfer.app\app\Boorstrap.php:262
Stack trace:
#0 [internal function]: Phalcon\Di\Service->resolve(NULL, Object(Phalcon\Di\FactoryDefault))
#1 [internal function]: Phalcon\Di->get('router', NULL)
#2 [internal function]: Phalcon\Di->getShared('router')
#3 C:\OpenServer\domains\Vtransfer.app\app\Boorstrap.php(262): Phalcon\Di->offsetGet('router')
#4 C:\OpenServer\domains\Vtransfer.app\app\Boorstrap.php(113): VTransfer\Bootstrap->dispatch(Object(Phalcon\Di\FactoryDefault))
#5 C:\OpenServer\domains\Vtransfer.app\public\index.php(20): VTransfer\Bootstrap->run()
#6 {main}
  thrown in C:\OpenServer\domains\Vtransfer.app\app\Boorstrap.php on line 262

on line 262, the function that is called at startup:
private function dispatch($di)
    {
        $router = $di['router'];

        $router->handle();

        $view = $di['view'];

        $dispatcher = $di['dispatcher'];

        $response = $di['response'];

        $dispatcher->setModuleName($router->getModuleName());
        $dispatcher->setControllerName($router->getControllerName());
        $dispatcher->setActionName($router->getActionName());
        $dispatcher->setParams($router->getParams());

        $moduleName = $this->camelize($router->getModuleName());

        $ModuleClassName = $moduleName . '\Module';

        if (class_exists($ModuleClassName))
        {
            $module = new $ModuleClassName;
            $module->registerAutoloaders();
            $module->registerServices($di);
        }

        $view->start();

        if (true)
        {
            $debug = new \Phalcon\Debug();
            $debug->listen();

            $dispatcher->dispatch();
        }
        else
        {
            try {
                $dispatcher->dispatch();
            } catch (\Phalcon\Exception $e) {
                // Errors catching

                $view->setViewsDir(__DIR__ . '/modules/Index/views/');
                $view->setPartialsDir('');
                $view->e = $e;

                if ($e instanceof \Phalcon\Mvc\Dispatcher\Exception) {
                    $response->setHeader(404, 'Not Found');
                    $view->partial('error/error404');
                } else {
                    $response->setHeader(503, 'Service Unavailable');
                    $view->partial('error/error503');
                }
                $response->sendHeaders();
                echo $response->getContent();
                return;

            }
        }

        $view->render(
            $dispatcher->getControllerName(),
            $dispatcher->getActionName(),
            $dispatcher->getParams()
        );

        $view->finish();

        $response = $di['response'];

        $response->sendHeaders();

        echo $response->getContent();
    }

Here is the Base Router:
use Phalcon\Mvc\Router;

class BaseRouter extends Router
{

    public function __construct()
    {
        parent::__construct();

        $this->setDefaultModule('index');
        $this->setDefaultController('index');
        $this->setDefaultAction('index');

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

        $this->add('/:module/:controller', array(
            'module' => 1,
            'controller' => 2,
            'action' => 'index',
        ))->setName('default_action');

        $this->add('/:module', array(
            'module' => 1,
            'controller' => 'index',
            'action' => 'index',
        ))->setName('default_controller');


        $this->removeExtraSlashes(true);
    }

    /**
     * @param $group \Phalcon\Mvc\Router\Group
     */
    public function addGroup($group)
    {
        $this->mount($group);
    }
}

And here is Routes.php which is in the folder with the module:
use Phalcon\Mvc\Router\Group as Group;

class Routes
{

    /**
     * @param $router \Phalcon\Mvc\Router
     * @return mixed
     */
    public function init($router)
    {
        /*
        $index = new Group(array(
            'module' => 'admin',
            'controller' => 'index'
        ));

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

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

        $index->add('/save', array(
            'controller' => 'users'
            'action' => 'save'
        ));

        $index->add('/edit/user/{id}', array(
            'controller' => 'users'
            'action' => 'edit'
        ));

        // Добавление группы в общие правила маршрутизации
        $router->mount($index);
        */
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
KorsaR-ZN, 2015-07-03
@aizhar777

What frame version?
In general, such an error occurs when your router is not correctly registered in the DI container, for example, the class does not exist.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question