munaz
munaz

Reputation: 166

Phalcon 4 multi module backend not loading

I'm in windows using php-7.4.1 Architecture-x64, phalcon - 4.0.2, psr - 0.7.0 and follow instruction from 'https://docs.phalcon.io/4.0/en/application' but the problem is its always render frontend module & its view. I'm unable to find out what i'm doing wrong?

[Index.php]

use Phalcon\Mvc\Router;
use Phalcon\Mvc\Application;
use Phalcon\Di\FactoryDefault;
$di = new FactoryDefault();
$di->set('router',function () {
        $router = new Router(false);
        $router->setDefaultModule('frontend');
        $router->add('/login',
            [
                'module'     => 'backend',
                'controller' => 'index',
                'action'     => 'index',
            ]
        );
        $router->add('/admin/products/:action',
            [
                'module'     => 'backend',
                'controller' => 'index',
                'action'     => 1,
            ]
        );

        return $router;
    }
);

$application = new Application($di);

$application->registerModules(
    [
        'frontend' => [
            'className' => \Multiple\Frontend\Module::class,
            'path'      => '../apps/frontend/Module.php',
        ],
        'backend'  => [
            'className' => \Multiple\Backend\Module::class,
            'path'      => '../apps/backend/Module.php',
        ],
    ]
);
try {
    $response = $application->handle($_SERVER["REQUEST_URI"]);
    $response->send();
} catch (\Exception $e) {
    echo $e->getMessage();
}

[Backend module]

<?php
namespace Multiple\Backend;
use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Di\DiInterface;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\ModuleDefinitionInterface;
class Module implements ModuleDefinitionInterface
{
    public function registerAutoloaders(DiInterface $di = null)
    {
        $loader = new Loader();
        $loader->registerNamespaces(
            [
                'Multiple\Backend\Controllers' => '../apps/backend/controllers/',
                'Multiple\Backend\Models'      => '../apps/backend/models/',
            ]
        );
        $loader->register();

    }
    public function registerServices(DiInterface $di)
    {
        $di->set('dispatcher',function () {
                $dispatcher = new Dispatcher();
                $dispatcher->setDefaultNamespace('Multiple\Backend\Controllers');
                return $dispatcher;
            }
        );
        $di->set('view',function () {
                $view = new View();
                $view->setViewsDir('../apps/backend/views/');
                return $view;
            }
        );
    }
}

[Index Controller]

<?php
namespace Multiple\Backend\Controllers;
use Phalcon\Mvc\Controller;
class IndexController extends Controller
{
    public function indexAction()
    {
        return '<h1>Back Controller!</h1>';
    }
}

Upvotes: 2

Views: 536

Answers (2)

The Oracle
The Oracle

Reputation: 2503

Make sure you have registered your new module in

../app/bootstrap_web.php around line 47 which looks as shown below

$application->registerModules([
    'frontend' => ['className' => 'MyApp\Modules\Frontend\Module'],
    // <--- add your new module here   --->

]);

and that your module class is also registered in the loader at ../app/config/loader.php around line 18 which looks as shown below

$loader->registerClasses([
'MyApp\Modules\Frontend\Module' => APP_PATH . '/modules/frontend/Module.php',
// <--- Add your new module class here --->
]);

Always keep an eye on your namespaces. I hope it helps.

Upvotes: 0

Ruud Boon
Ruud Boon

Reputation: 51

Did you set your namespaces in the frontend module as well? Like you did with registerAutoloaders in the backend.

Upvotes: 2

Related Questions