Phalcon 2.如果路径没有找到,那么带有模块的框架应用不会产生404 Not Found


Phalcon 2.x skeleton app with modules doesn't produce 404 Not Found if path is not found

我已经使用开发工具生成了一个骨架应用程序,它只有一个模块。我所遇到的问题是,无论我在浏览器中键入什么URL -它总是返回apps/frontend/views/index.volt的内容(前端是模块名称)。

这是我的services.php

<?php
/**
 * Services are globally registered in this file
 *
 * @var 'Phalcon'Config $config
 */
use Phalcon'Mvc'Router;
use Phalcon'Mvc'Url as UrlResolver;
use Phalcon'Di'FactoryDefault;
use Phalcon'Session'Adapter'Files as SessionAdapter;
use Phalcon'Db'Adapter'Pdo'Mysql as DbAdapter;
use Phalcon'Mvc'Model'Metadata'Memory as MetaDataAdapter;
use Phalcon'Mvc'View;
use Phalcon'Mvc'View'Engine'Volt as VoltEngine;
/**
 * The FactoryDefault Dependency Injector automatically registers the right services to provide a full stack framework
 */
$di = new FactoryDefault();
/**
 * Registering a router
 */
$di->set('router', function () {
    $router = new Router();
    $router->setDefaultModule('frontend');
    $router->setDefaultNamespace('Homediary'Frontend'Controllers');
    return $router;
});
/**
 * The URL component is used to generate all kinds of URLs in the application
 */
$di->set('url', function () {
    $url = new UrlResolver();
    $url->setBaseUri('/Homediary/');
    return $url;
});
/**
 * Setting up the view component
 */
$di->setShared('view', function () use ($config) {
    $view = new View();
    $view->setViewsDir($config->application->viewsDir);
    $view->registerEngines(array(
        '.volt' => function ($view, $di) use ($config) {
            $volt = new VoltEngine($view, $di);
            $volt->setOptions(array(
                'compiledPath' => $config->application->cacheDir,
                'compiledSeparator' => '_',
                'stat' => true,
                'compileAlways' => true
            ));
            return $volt;
        },
        '.phtml' => 'Phalcon'Mvc'View'Engine'Php'
    ));
    return $view;
});
/**
 * Database connection is created based in the parameters defined in the configuration file
 */
$di->set('db', function () use ($config) {
    return new DbAdapter($config->database->toArray());
});
/**
 * If the configuration specify the use of metadata adapter use it or use memory otherwise
 */
$di->set('modelsMetadata', function () {
    return new MetaDataAdapter();
});
/**
 * Starts the session the first time some component requests the session service
 */
$di->setShared('session', function () {
    $session = new SessionAdapter();
    $session->start();
    return $session;
});
/**
* Set the default namespace for dispatcher
*/
$di->setShared('dispatcher', function() use ($di) {
    $dispatcher = new Phalcon'Mvc'Dispatcher();
    $dispatcher->setDefaultNamespace('Homediary'Frontend'Controllers');
    return $dispatcher;
});

和routes.php

<?php
$router = $di->get("router");
foreach ($application->getModules() as $key => $module) {
    $namespace = str_replace('Module','Controllers', $module["className"]);
    $router->add('/'.$key.'/:params', array(
        'namespace' => $namespace,
        'module' => $key,
        'controller' => 'index',
        'action' => 'index',
        'params' => 1
    ))->setName($key);
    $router->add('/'.$key.'/:controller/:params', array(
        'namespace' => $namespace,
        'module' => $key,
        'controller' => 1,
        'action' => 'index',
        'params' => 2
    ));
    $router->add('/'.$key.'/:controller/:action/:params', array(
        'namespace' => $namespace,
        'module' => $key,
        'controller' => 1,
        'action' => 2,
        'params' => 3
    ));
}
/*
//Set 404 paths
$router->notFound(array(
    "controller" => "index",
    "action"     => "notFoundAction"
));
*/

和nginx config

 server {
   listen                *:80;
   server_name           homediary.dev www.homediary.dev;
   client_max_body_size 100m;
   root /var/www/public;
     index  index.html index.htm index.php;
   access_log            /var/log/nginx/nxv_tygjjhwtk0si.access.log;
   error_log             /var/log/nginx/nxv_tygjjhwtk0si.error.log;
   location / {
     root  /var/www/public;
     try_files $uri $uri/ /index.php;
     autoindex off;
     index  index.html index.htm index.php;

   }
   location ~ '.php$ {
     root  /var/www/public;
     fastcgi_index index.php;
     fastcgi_split_path_info ^(.+'.php)(/.*)$;
     #try_files $uri $uri/ /index.php$is_args$args;
     try_files $uri =404;
     include /etc/nginx/fastcgi_params;
     fastcgi_pass 127.0.0.1:9000;
     fastcgi_param SCRIPT_FILENAME $request_filename;
     fastcgi_param APP_ENV dev;
   }
   sendfile off;
 }

问题是在nginx配置传递路由在REQUEST_URI vs一个特殊的_url变量。为了让Phalcon在这个设置下工作,我必须添加

$router->setUriSource(Router::URI_SOURCE_SERVER_REQUEST_URI);

之后

$router = new Router();

然后开始正常工作:)