Phalcon | nginx | php5-fpm返回错误的控制器

时间:2014-09-11 17:07:00

标签: nginx controller phalcon

暂时坚持这个问题,让我失去动力:(。努力保持积极。

点击端点/注册时,我无法点击正确的控制器。它改为返回indexController。

我已就同一主题搜集了各种问题。

这个答案不起作用:Phalcon and nginx - framework run only indexController

的index.php

<?php
try {
    //Register an autoloader
    $loader = new \Phalcon\Loader();
    $loader->registerDirs(array(
        '../application/controllers/',
        '../application/models/'
    ))->register();

    $di = new Phalcon\DI\FactoryDefault();

    //Setup the view component
    $di->set('view', function(){
        $view = new \Phalcon\Mvc\View();
        $view->setViewsDir('../application/views/');
        return $view;
    });

    $di->set('url', function(){
        $url = new Phalcon\Mvc\Url();
        $url->setBaseUri('/');
        return $url;
    });

    $di->set('router', function() {
        $router = new \Phalcon\Mvc\Router();
        $router->setUriSource('/');
        $router->add(
            'signup',
            array(
                "controller" => "/signup",
                "action" => "index"
            )
        );
        $router->handle();
        return $router;
    });

    if (!empty($_SERVER['REQUEST_URI'])) {
        $pathInfo = $_SERVER['REQUEST_URI'];
    } else {
        $pathInfo = '/';
    }


    //Handle the request
    $application = new \Phalcon\Mvc\Application($di);
    echo $application->handle($pathInfo)->getContent();

} catch(\Phalcon\Exception $e) {
    echo "PhalconException: ", $e->getMessage();
}

nginx config(网站可用)

server {
    listen   80;
    server_name  Tikarta-berkshelf;
    root /var/www/public;
    index index.php;
    access_log  /var/log/nginx/localhost.access.log;

    location / {
    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 ~ \.php$ {
    #try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/var/run/php5-fpm.sock;

    fastcgi_index index.php;
    include fastcgi_params;
    }
    location ~ /\.ht {
    deny all;
    }
}

1 个答案:

答案 0 :(得分:0)

首先,您不需要$pathInfo删除此内容:

if (!empty($_SERVER['REQUEST_URI'])) {
    $pathInfo = $_SERVER['REQUEST_URI'];
} else {
    $pathInfo = '/';
}

并更新$application->handle()

echo $application->handle()->getContent();

Phalcon知道在没有指定的情况下该怎么做。您的路由配置中也有拼写错误:

$router->add(
    'signup',
    array(
        "controller" => "/signup",
        "action" => "index"
    )
);

必须是这个(斜线在错误的地方):

$router->add(
    '/signup',
    array(
        "controller" => "signup",
        "action" => "index"
    )
);

如果这没有帮助,您需要进一步消除问题。如果你的nginx配置是正确的,你应该总是有你的请求URL的$ _GET ['_ url']值。如果没有设置,请尝试使用官方文档示例更新nginx配置,并且不要忘记重新启动它以尝试。