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

Second module cannot be added

Hi,

I created a new phalcon project with devtools:

phalcon project box_wcs --type=modules

The project has been created with the standard module 'frontend'. I want to add another module named 'backend' and used therefore this command:

phalcon module --name backend --namespace=Box_wcs\ --output apps/

This created a new module named 'backend' inside the apps directory

After that I edited the apps/backend/Module.php, config/modules.php and the config/routes.php

The apps/backend/Modules.php looks like the following:

<?php

namespace Box_wcs\Backend;

use Phalcon\DiInterface;
use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\ModuleDefinitionInterface;
use Phalcon\Config;

class Module implements ModuleDefinitionInterface
{
    /**
     * Registers an autoloader related to the module
     *
     * @param DiInterface $di
     */
    public function registerAutoloaders(DiInterface $di = null)
    {
        $loader = new Loader();

        $loader->registerNamespaces(array(
            'Box_wcs\Backend\Controllers' => __DIR__ . '/controllers/',
            'Box_wcs\Backend\Models'      => __DIR__ . '/models/'
        ));

        $loader->register();
    }

    /**
     * Registers services related to the module
     *
     * @param DiInterface $di
     */
    public function registerServices(DiInterface $di)
    {
        /**
         * Read common configuration
         */
         $config = $di->has('config') ? $di->getShared('config') : null;

        /**
         * Try to load local configuration
         */
        if (file_exists(__DIR__ . '/config/config.php')) {
            $override = new Config(include __DIR__ . '/config/config.php');;

            if ($config instanceof Config) {
                $config->merge($override);
            } else {
                $config = $override;
            }
        }

        //Registering a dispatcher
        $di['dispatcher'] = function () use ($di) {
            $dispatcher = new Dispatcher();
            $dispatcher->setDefaultNamespace("Box_wcs\Backend\Controllers");
            //            $dispatcher->setEventsManager($di->get('eventsManager'));
            return $dispatcher;
        };

        /**
         * Setting up the view component
         */
        $di['view'] = function () use ($config) {
            $view = new View();
            $view->setViewsDir($config->get('application')->viewsDir);

            return $view;
        };

        /**
         * Database connection is created based in the parameters defined in the configuration file
         */
        $di['db'] = function () use ($config) {
            $config = $config->database->toArray();

            $dbAdapter = '\Phalcon\Db\Adapter\Pdo\\' . $config['adapter'];
            unset($config['adapter']);

            return new $dbAdapter($config);
        };
    }
}

The config/modules.php:


<?php

/**
 * Register application modules
 */
$application->registerModules(array(
    'frontend' => array(
        'className' => 'Box_wcs\Frontend\Module',
        'path' => __DIR__ . '/../apps/frontend/Module.php'
    ),
    'backend' => array(
        'className' => 'Box_wcs\Backend\Module',
        'path' => __DIR__ . '/../apps/backend/Module.php'
    )
));

And the config/routes.php


<?php

$router = $di->get("router");

foreach ($application->getModules() as $key => $module) {
    $namespace = str_replace('Module','Controllers', $module["className"]);
    $router->add('/'.$key.'/:params', array(
        'namespace' => $namespace,
        'module' => $key,
        'controller' => 'index',
        'action' => 'index',
        'params' => 1
    ))->setName($key);
    $router->add('/'.$key.'/:controller/:params', array(
        'namespace' => $namespace,
        'module' => $key,
        'controller' => 1,
        'action' => 'index',
        'params' => 2
    ));
    $router->add('/'.$key.'/:controller/:action/:params', array(
        'namespace' => $namespace,
        'module' => $key,
        'controller' => 1,
        'action' => 2,
        'params' => 3
    ));
    $router->add('/'.$key.'/:controller/:action', array(
            'namespace' => $namespace,
            'module' => $key,
            'controller' => 1,
            'action' => 2
    ));
}

$di->set("router", $router);

I also creted an IndexController in apps/backend/controllers and a index.phtml in apps/backend/views/index.

But everytime I invoke https://localhost/box_wcs/backend I cannot see anything, just a blank page. https://localhost/box_wcs or https://localhost/box_wcs/frontend work both fine.

Do you have any hint for me?

What you mean blank page ? What is in view ? Maybe it's just empty ? Maybe there is some error ? Check php logs.

It means that there is only a white blank page with no content. There exists a file in views/index/index.phtml. This file should be invoked when I access https://localhost/box_wcs/backend, should'nt it?

Unfortunately no error is displayed. At the moment I have no access to php logs, I can give it to you later.

Thanks



47.7k

In your apps/backend/Modules.php I see:

$view->setViewsDir($config->get('application')->viewsDir);

I have:

$view->setViewsDir($config->backend->viewsDir);

Have you been here? https://github.com/phalcon/mvc

Hi,


$config->get('application')->viewsDir

must be correct. It prints out the correct path to the views directory

I also tried


__DIR__ . '/views/';

which also points to the correct directory, but is also not working

I had a look to apache2 error.log which printed out:

[Mon Sep 12 20:08:54.142098 2016] [:error] [pid 2360] [client 10.0.2.2:64652] PHP Fatal error: Class 'Box_wcs\Backend\Dispatcher' not found in /home/thomas/workspace/box_wcs/box_wcs/apps/backend/Module.php on line 58

What does this mean and where is the mistake I made?

Yeah, I had a look at https://github.com/phalcon/mvc but I couldn't found the part I was making wrong. Next step would be to download a working example from there, but I first wanted to try the own/devtools method. There should be no reason why it should not work.



145.0k
Accepted
answer
edited Sep '16
  1. Use IDE.
  2. Add use Phalcon\Mvc\Dispatcher; in apps/backend/Module.php, you are missing it, if you would use IDE it would inform you about it

Your're right. This was the mistake. Thank you!

Discussion can be closed!