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

Multiple Service Layer Model

Hi,

I want to use a multi apps architecture who content a Phalcon Application and a Micro Application for my rest API. I want too than theses two apps use the same models folder. So I have to use this architecture right ? But I don't know if this example on your github still works, because there was no update since 2 years ago. And I don't want multiple models folder as entities/repositories/services because I don't know their value. I just want once models folder for my two apps.

So could you tell me if the multiple service layer model still works and if he is good for what I want to do ?

Thanks.



145.0k
Accepted
answer

If you dont want multiple models folder then just put all your models repositories for example under app/models and/or app/repositories and register them in loader before registering modules.

Thanks for your answer. Ok I have this :

multiple/
├── apps
│   ├── config
│   │   ├── config.php
│   │   ├── modules.php
│   │   └── services.php
│   ├── models
│   │   └── User.php
│   └── modules
│       └── frontend
│       │   ├── Module.php
│       │   ├── controllers
│       │   │   ├── ControllerBase.php
│       │   │   └── IndexController.php
│       │   ├── plugins
│       │   │   └── SecurityPlugin.php
│       │   │   └── NotFoundPlugin.php
│       │   ├── library
│       │   │   └── Elements.php
│       │   │   └── Tools.php
│       │   ├── cache
│       │   │   └── cache1.php
│       │   │   └── cache2.php
│       │   └── views
│       │       ├── index
│       │       │   └── index.phtml
│       │       └── index.phtml
│       │
│       └── api
│           ├── index.php
│       
├── .htaccess
├── index.html
├── public
    └── index.php
    └── .htaccess

It's ok ? But this is my first multi apps with phalcon. And this is really hard for me to make the config. If you have an example for the config folder this will be nice.

I would remove apps folder, and put creat app folder under multiple and put there apps/config and apps/models and put apps/modules under multiple/modules. Also isn't library something you will be using in every module ? I think it should be in multiple/library but not sure what you put here. If it's for example swift mailer then for sure it should go something to like multiple/library or multiple/vendor

I don't understand why I have to remove the apps folder and rename it by app. Because in the documentation the multi module should has an apps folder. I forgot to put Module.php under my REST-API.

I think I have to create 2 folders library because frontend and api will use the same libraries and certain libraries will be used by just the frontend. For example Elements.php is the folder to create the elements in the frontend navbar then in my API I don't need it. But Tools.php will be used by them both.

edited Apr '16

You mean this ?

multiple/
├── modules
│   ├── config
│   │   ├── config.php
│   │   ├── modules.php
│   │   └── services.php
│   ├── library
│   │   └── Elements.php
│   │   └── Tools.php
│   │   └── PhpMailer.php
│   ├── models
│   │   └── User.php
│   ├── frontend
│   │   ├── Module.php
│   │   ├── controllers
│   │   │   ├── ControllerBase.php
│   │   │   └── IndexController.php
│   │   ├── plugins
│   │   │   └── SecurityPlugin.php
│   │   │   └── NotFoundPlugin.php
│   │   ├── cache
│   │   │   └── cache1.php
│   │   │   └── cache2.php
│   │   └── views
│   │       ├── index
│   │       │   └── index.phtml
│   │       └── index.phtml
│   │
│   └── api
│       └── index.php
│       └── Module.php
│       
├── .htaccess
├── index.html
└── public
    └── index.php
    └── .htaccess

Almost, but put config, library and models outside of modules into seperate folder like app, beacause they are not modules.

Okay like this :

multiple/
├── common
│   ├── config
│   │   ├── config.php
│   │   ├── modules.php
│   │   └── services.php
│   ├── library
│   │   └── Elements.php
│   │   └── Tools.php
│   │   └── PhpMailer.php
│   ├── models
│   │   └── User.php
├── modules
│   ├── frontend
│   │   ├── Module.php
│   │   ├── controllers
│   │   │   ├── ControllerBase.php
│   │   │   └── IndexController.php
│   │   ├── plugins
│   │   │   └── SecurityPlugin.php
│   │   │   └── NotFoundPlugin.php
│   │   ├── cache
│   │   │   └── cache1.php
│   │   │   └── cache2.php
│   │   └── views
│   │       ├── index
│   │       │   └── index.phtml
│   │       └── index.phtml
│   │
│   └── api
│       └── index.php
│       └── Module.php
│       
├── .htaccess
├── index.html
└── public
    └── index.php
    └── .htaccess

It is possible to help me to build the config files and modules files code ?

Yea something like this, but sorry for my mistake, i would put library(if it's really library like something to pdf or emails not related to phalcon) outside of common just to multiple/library or rename it to vendor and put it to multiple/vendor.

It is possible to help me to build the config files and modules files code ?

What you mean ?

Ok thanks. I don't know what should I do in the Bootstrap (public/index.php), config folder (config.php, services.php, modules.php) and Module.php (modules/api/Module.php, modules/frontend/Module.php). I don't find any examples for this architecture so I don't know what to set in the registerModules, registerClasses etc... I don't know what code I can set in theses files.

On config.php(i prefer ini file) make configuration like database config etc. In services.php register your services, in modules write modules you have, in bootstrap register modules, require services file, load model classes. In Module.php you store your module definition and register classes and services you need in module. All examples are here : https://github.com/phalcon/mvc

I already tried to download this mvc project and try to make it work but theses projects doesn't work because I have this error : PHP Fatal error: Class 'Acl' not found in /var/www/html/multiples/apps/frontend/Module.php on line 39. Theses projects are not finished :/

edited Apr '16

In which project(which type) you have this error ? They are only examples but they should work. You don't need to download them to make your files though.

I tried this project there is a problem with the ALC. I removed it and it works. Then the forward in the ProductsController should looks like this :

        $this->dispatcher->forward(
                array(
                        "controller" => "login",
                        "action"     => "index"
                )
        );

instead of :

return $this->response->redirect('login');

because this redirect in /public/login.

Now I'll try to make the architecture with the common models.

edited Apr '16

Api is a Micro application, there is just a index.php for my rest api, then the Module.php in api folder is useless right ? Or should I put all my rest api in a controller ?

There are no controllers in micro application.

edited Apr '16

Then there is no Module.php for the api folder ? Just an index.php ? Maybe it is useless to make a micro module in multi module to create web services ? Because I can't define routes if there is not controller.

I'll make another thread on the forum because you answered to my question. Thanks a lot :)