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

Having more than one Controller / Model / View directories for different domains (or other reasons).

In CakePHP there is a feature where you can setup a hierarchy of directories for pretty much anything. It works by scanning each directory until it hits the file it needs based on the route. I'm toying with the idea of having one main app and harbouring custom code in the same place.

Example

/app
    /controllers
        /acme.example.com
    /models
    /views
        /acme.example.com
    /public

So say I have an application that covers mostly what clients want but some of the clients have requested a specific feature. Rather than duplicate the entire app and re-host it I would like to tell Phalcon use directory A and B for controllers (or whatever) does Phalcon have such a feature? I only seem to be able to find examples of creating a backend and a frontend.



2.0k

U can do it with events, like in the INVO security.

I think if u create a beforeDispatch event, u can check if u have an extended controller for that project, and than u can forward the request to that controller :)

I hope it's help for u.



10.9k
edited May '14

Well that does give me an option to help me make sure the app makes decisions but what I would like to be able to do is "include" the sub directory in as ALSO the controllers directory.

So, if I have the following:

/app
    /controllers
        /acme.example.com
        AcmeController.php
    OrdersController.php
    AccountsController.php

I want to say: "Look in /controllers first and then /acme.example.com if you can't find it." So www.example.com/orders/myaction and acme.example.com/orders/myaction would both be the same but acme.example.com/acme/myaction would also work for that sub domain because once it didn't find the controller Phalcon moved on to the next folder it has registered. Currently I can only get Phalcon to accept one folder and that's that. I want to have in my config a routine that includes any folder with the same name as the url so all that needs to be done is to upload a folder and then version control on the core app is much easier. This could turn out to be a rubbish idea I realise but it's my first one.

In your bootstrap file I assume you have something like this:

$Loader = new \Phalcon\Loader();
$Loader->registerDirs($Config->phalcon->load_dirs->toArray());
// put code here
$Loader->register();

to load your controller directory?

Where I've written put code here you could add code to determine the current requested domain name, then dynamically add it as a directory. Something like (this is pseudo-code)

$domain_name = $_SERVER[‘DOMAIN’];
if(file_exists(‘/path/to/controller/dir/‘.$domain_name)){
    $Loader->registerDirs(‘/path/to/controller/dir’.$domain_name);
}