Phalcon ajenti配置在我的网站上运行多个模块

时间:2015-09-12 07:05:31

标签: php nginx phalcon

我倾向于使Phalcon使用多个模块。 使用struct。

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

和nginx配置:

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;

    }

}

在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;
});

当我访问localhost.dev时:

打印:Hello Frontend!

但是,我访问localhost.dev/frontend

它打印:无法加载Frontend \ Controllers \ BackendController处理程序类

如何解决?

其他问题。

我应该在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; 
    } 
}

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;
        }

1 个答案:

答案 0 :(得分:1)

对于您的第一个问题,我建议您使用Group of Routes以获得更好的表现 一个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;
}

此外,您可以使用命名空间来防止相似名称冲突。

祝你好运