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.

How to structure a project like this?

Hi,

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:

$loader->registerNamespaces([

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

$loader->register();

In services:

$di->set('dispatcher', function () {
    $dispatcher = new Dispatcher();
    $dispatcher->setDefaultNamespace('MyApp\Controllers');
    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.

Thanks!

edited 16d ago

https://docs.phalconphp.com/en/3.0.0/reference/namespaces.html#

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:

localhost/website/stats

$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 16d ago

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



91.3k
Accepted
answer
edited 16d ago

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.phalconphp.com/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