Tiến Mạnh
Tiến Mạnh

Reputation: 117

Phalcon ajenti config to run multiple module on my site

I'm leaning Phalcon with multiple module. With struct.

- apps
    - frontend
        controllers
        models
        views
        Modules.php
    - backend
        controllers
        models
        views
        Modules.php
- public
    css
    js
    img
    index.php

And nginx config:

server {
    listen *:80 default_server;


    server_name localhost.dev;

    access_log /var/log/nginx/localhostdev.access.log;
    error_log /var/log/nginx/localhostdev.error.log;

    root /srv/localhost.dev/public;
    index index.html index.htm index.php;

       set $root_path '/srv/localhost.dev/public';

    location / {
        root   $root_path;
        index  index.php index.html index.htm;

        # if file exists return it right away
        if (-f $request_filename) {
            break;
        }

        # otherwise rewrite it
        if (!-e $request_filename) {
            rewrite ^(.+)$ /index.php?_url=$1 last;
            break;
        }
    }

    location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        root $root_path;
    }



    location ~ [^/]\.php(/|$) {



        fastcgi_index index.php;
        include fcgi.conf;
        fastcgi_pass unix:/var/run/ajenti-v-php-fcgi-localhostdev-php-fcgi-0.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    }

}

In public/index.php:

$di->set('router', function ()
{
    $router = new Router();

    $router->setDefaultModule("frontend");

    $router->add('/:module/:controller/:action/:params', array(
        'module'     => 1,
        'controller' => 2,
        'action'     => 3,
        'params'     => 4
    ));
    return $router;
});

When i access localhost.dev:

it print: Hello Frontend!

But, i access localhost.dev/frontend

it print: Frontend\Controllers\BackendController handler class cannot be loaded

How to fix it?

Additional question.

What I should config in nginx:

root /srv/localhost.dev;
# nginx configuration 
location / { 
    rewrite ^/$ /public/ break; 
    rewrite ((?s).*) /public/$1 break; 
}

location /public/ { 
    if (!-e $request_filename){ 
        rewrite ^/((?s).*)$ /index.php?_url=/$1 break; 
    } 
}

or

root /srv/localhost.dev/public;
 # if file exists return it right away
        if (-f $request_filename) {
            break;
        }

        # otherwise rewrite it
        if (!-e $request_filename) {
            rewrite ^(.+)$ /index.php?_url=$1 last;
            break;
        }

Upvotes: 3

Views: 200

Answers (1)

M2sh
M2sh

Reputation: 761

For your first question, I suggest You to use Group of Routes for better expreience.
An expamle :

$di->set('router', function ()
{
    $router = new Router();
    $router->setDefaultNamespace('your Frontend namespace');
    $router->setDefaultModule('frontend');

    $frontend = new \Phalcon\Mvc\Router\Group(array(
       'module' => 'frontend',
       'namespace' => 'Your frontend NameSpace',
    ));

    $frontend->setPrefix('/frontend');


    $frontend->add('/:controller/:action/:params',array(
       'controller' => 1,
       'action' => 2,
       'params' => 3
    ));

    $router->mount($frontned);

    $backend = new \Phalcon\Mvc\Router\Group(array(
       'module' => 'backend',
       'namespace' => 'Your backend NameSpace',
    ));

    $backend->setPrefix('/backend');


    $backend->add('/:controller/:action/:params',array(
       'controller' => 1,
       'action' => 2,
       'params' => 3
    ));

    $router->mount($backend);

    return $router;
}

Also, you could using Namespaces to prevent from conflicting similar names.

Good Luck

Upvotes: 1

Related Questions