Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A multi module MVC structure in phalconphp

Tags:

php

phalcon

Hi I'm trying to implement a Multi Module MVC for frontend and backend like what is in phalconphp documentations. But I can't make it work. it's about an hour but I really can not understand where is the problem.

Could anyone guide me how can I make a skeleton for a multi module mvc for frontend and backend.

what should I put in Moudle.php for frontend and backend
And also what should I put in bootstrap file located in public/index.php
And any extra file or information I need.

like image 585
Pars Avatar asked Mar 19 '23 20:03

Pars


1 Answers

The code in the phalcon/mvc repository on GitHub will help. You can find it here: https://github.com/phalcon/mvc/tree/master/multiple

More specifically, you'll be interested in:

https://github.com/phalcon/mvc/blob/master/multiple/public/index.php https://github.com/phalcon/mvc/blob/master/multiple/apps/backend/Module.php

I tend to use this in my index.php:

$application = new \Phalcon\Mvc\Application($di);

// Register the installed modules
$application->registerModules(
    array(
        'web' => array(
            'className' => 'Apps\Web\Module',
            'path'      => '../apps/web/Module.php',
        )
    )
);

echo $application->handle()->getContent();

And in my Module.php:

<?php

namespace Apps\Web;

use Phalcon\Loader;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\View;
use Phalcon\Mvc\ModuleDefinitionInterface;

class Module implements ModuleDefinitionInterface
{

    /**
     * Register a specific autoloader for the module
     */
    public function registerAutoloaders()
    {

        $loader = new Loader();

        $loader->registerNamespaces(
            array(
                'Apps\Web\Controllers' => '../apps/web/controllers/',
            )
        );

        $loader->register();
    }

    /**
     * Register specific services for the module
     * @param \Phalcon\DI\FactoryDefault $di
     */
    public function registerServices($di)
    {
        //Registering a dispatcher
        $di->set(
            'dispatcher',
            function() use ($di) {
                $eventsManager = $di->getShared('eventsManager');
                $dispatcher = new Dispatcher();
                $dispatcher->setDefaultNamespace('Apps\Web\Controllers');
                $eventsManager->attach(
                    'dispatch:beforeException',
                    function($event, $dispatcher, $exception) use ($di) {
                        /* @var $dispatcher \Phalcon\Mvc\Dispatcher */
                        switch ($exception->getCode()) {
                            case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                            case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                                $di->set('lastException', $exception);
                                $dispatcher->forward(
                                    array(
                                        'module' => 'web',
                                        'controller' => 'error',
                                        'action' => 'notFound',
                                    )
                                );
                                return false;
                            default:
                                $di->set('lastException', $exception);
                                $dispatcher->forward(
                                    array(
                                        'module' => 'web',
                                        'controller' => 'error',
                                        'action' => 'uncaughtException',
                                    )
                                );
                                return false;
                        }
                    }
                );
                $dispatcher->setEventsManager($eventsManager);
                return $dispatcher;
            }
        );
    }

}

Hopefully this helps!

like image 78
Roger Thomas Avatar answered Mar 28 '23 18:03

Roger Thomas