Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

index by default not load on multi module project

Hi!, i have a little problem,

http://mcets-inc.com/Selecci%C3%B3n%5376.png

i configured my project, with multi module structure, when i try to access to: http://feedback/public/index the error page load, but if i try with: http://feedback/public/index/index the real page load, so, what is the problem? this is my bootstrap conf:

<?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->notFound(array(
            'module' => 'backend',
            'controller' => 'error',
            '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();
}

Any Help please!



12.6k
Accepted
answer
edited Apr '15

Hello,

The only problem is that phalcon doesn't know to get the default controller, action on multimodule applications. In this case you have to define a route for each. So in your routes add this too

// for http://feedback/public/index
$router->add('/:module/:controller', array(
        'module' => 1,
        'controller' => 2,
        'action' => 'index',
));

// for http://feedback/public
$router->add('/:module/:controller', array(
        'module' => 1,
        'controller' => 'index',
        'action' => 'index',
));

You could also try adding a default Controller

$router->setDefaultController('index');

Thanks a Lot! , this works for me!. // for http://feedback/public/index $router->add('/:module/:controller', array( 'module' => 1, 'controller' => 2, 'action' => 'index', ));