We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Multi Module Project setDefaults Route problem

Hi, i have and other problem with multi module project, i cant access from the base url: https://https://feedback/ i defined this route:

$router->setDefaults(array(
                'module' => 'backend',
                'controller' => 'login',
                'action' => 'index'
            ));

but if i access from: https://feedback/backend/login this work!.. any Help please?

this is my bootstrap.

          <?php

  error_reporting(E_ALL);

  try {
      define('SITE_DOMAIN', $_SERVER['HTTP_HOST']);

      /**
       * Read the configuration
       */
      $config = include(__DIR__ . '/../app/config/' . SITE_DOMAIN . '_db.php');

      /**
       * The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
       */
      $di = new \Phalcon\DI\FactoryDefault();

      /**
       * The URL component is used to generate all kind of urls in the application
       */
      $di->set('url', function() use ($config) {
          $url = new \Phalcon\Mvc\Url();
          $url->setBaseUri($config->application->baseUri);
          return $url;
      });

      /**
       * Database connection is created based in the parameters defined in the configuration file
       */
      $di->set('db', function() use ($config) {
          return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
              "host" => $config->database->host,
              "username" => $config->database->username,
              "password" => $config->database->password,
              "dbname" => $config->database->name
          ));
      });

      /**
       * If the configuration specify the use of metadata adapter use it or use memory otherwise
       */
      $di->set('modelsMetadata', function() use ($config) {
          if (isset($config->models->metadata)) {
              $metadataAdapter = 'Phalcon\Mvc\Model\Metadata\\' . $config->models->metadata->adapter;
              return new $metadataAdapter();
          } else {
              return new \Phalcon\Mvc\Model\Metadata\Memory();
          }
      });

      /**
       * Start the session the first time some component request the session service
       */
      $di->set('session', function() {
          $session = new \Phalcon\Session\Adapter\Files();
          $session->start();
          return $session;
      });

      //Specify routes for modules
      $di->set('router', function () {
          $router = new \Phalcon\Mvc\Router(false);
          $router->setDefaultModule("backend");
          $router->setDefaultAction('index');

          $router->setDefaults(array(
              'module' => 'backend',
              'controller' => 'login',
              'action' => 'index'
          ));
          $router->notFound(array(
              'module' => 'backend',
              'controller' => 'error',
              'action' => 'index'
          ));

          $router->add('/:module/:controller', array(
              'module' => 1,
              'controller' => 2,
              'action' => 'index',
          ));
          $router->add('/:module/:controller/:action/:params', array(
              'module' => 1,
              'controller' => 2,
              'action' => 3,
              'params' => 4
          ));
          return $router;
      });
      /**
       * Handle the request
       */
      $application = new \Phalcon\Mvc\Application();
      $application->setDI($di);
      // Register the installed modules
      $application->registerModules(
              array(
                  'public' => array(
                      'className' => 'Frontend\Module',
                      'path' => '../app/frontend/Module.php',
                  ),
                  'backend' => array(
                      'className' => 'Backend\Module',
                      'path' => '../app/backend/Module.php',
                  )
              )
      );

      echo $application->handle()->getContent();
  } catch (Phalcon\Exception $e) {
      echo $e->getMessage();
  } catch (PDOException $e) {
      echo $e->getMessage();
  }


25.4k
Accepted
answer
edited Apr '15

I add, this code:

    $router->add('/', array(
            'module' => 'backend',
            'controller' => 'login',
            'action' => 'index',
    ));

and works!!, this is the only solutions to set de default load page, when access from base url?



16.0k
Accepted
answer

you set the default routing behavior on false. That means you have to define all routes yourself

$router = new \Phalcon\Mvc\Router(false);

you can try to set a default controller too and see if it works. I didn't tried so I'm not sure :D

$router->setDefaultController("index");

set default module in the bootsrap file

$router->setDefaultModule("default-module");

he already did as 'backend' if you look over his code

his problem is because he disabled the default routing behavior. The only way the '/' route will work only if he enable the default route behavior or define the route by itself

thanks!, i solved the problem with:

   $router->add('/', array(
        'module' => 'backend',
        'controller' => 'login',
        'action' => 'index',
));