We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

How to structure a project like this?


I started a project but it seems that I need a structure like this on it because I have many Controllers and Modules and Views: https://i.stack.imgur.com/72SAX.png

I tried to structure the project but I don't know exactly how to manage this with namespaces and routes.

I have something like this in config file:

    'application' => [
        'controllersDir' => APP_PATH . '/controllers/',
        'modelsDir'      => APP_PATH . '/models/',
        'formsDir'       => APP_PATH . '/forms/',
        'viewsDir'       => APP_PATH . '/views/',
        'helperDir'      => APP_PATH . '/helper/',
        'libraryDir'     => APP_PATH . '/library/',
        'pluginsDir'     => APP_PATH . '/plugins/',
        'cacheDir'       => BASE_PATH . '/cache/',]

In loader:


    'MyApp\Models'      => $config->application->modelsDir,
    'MyApp\Controllers' => $config->application->controllersDir,
    'MyApp\Forms'       => $config->application->formsDir,
    'MyApp'             => $config->application->libraryDir


In services:

$di->set('dispatcher', function () {
    $dispatcher = new Dispatcher();
    return $dispatcher;

I see this on the internet https://github.com/phalcon/mvc/tree/master/simple-subcontrollers but I still try to understand what we need to manage the namespaces and the routes, is someone here who can explain me in a simple way easy to understand or if you have another structure as example maybe will help others not only me. What I see here it is that https://github.com/phalcon/mvc/tree/master/simple-subcontrollers every folder has a ControllerBase inside so I don't need to make for every folder a ControllerBase I don't want to make 100 ControllerBases. How to use routes and namespaces to send the information corectly to every view even if we have folders inside too.


edited May '17


Pretty much you can need to specify it implicty when adding route - or setting setDefaultNamespace in router service. Though maybe in dispatcher it's enough.


Ok, let's say I will dezactivate the default routes like: $router = new Phalcon\Mvc\Router(false);

I will define the routes let's say:


$router->add('/:controller/:action', [ 'namespace' => 'MyApp\ControllersDirAdmin', / /controllers/admin/StatsController.php / 'controller' => 1 'action' => 2 ]);

$router->add('/:controller', [ 'namespace' => 'MyApp\ControllersDirAdmin', / /controllers/admin/StatsController.php / 'controller' => 1 ]);

After that how to set the view for: views/admin/stats/index.php

View has nothing to do with router or dispatcher. It will just look for controllerName/actionName.extension in view base dir.


Ok, I understand and if I will have a structure like this in views? How to send the information from controllers/admin/statsController.php to views/admin/stats/index.php so I have controller statsController and action index.

edited May '17

Or when I setup the routes admin/:controller/:action then it will look for admin/controllerName/actionName.extension in view base dir.

edited May '17

No, it will always look for controllerName/actionName.extension in view base dir. If you will have some more categories there then you just need to pick view by yourself. Just read docs https://docs.phalcon.io/en/3.0.2/reference/views.html#integrating-views-with-controllers everything is there :)

Just most likely you want $this->view->pick("admin/controllerName/actionName"); in action