2015-06-09 21 views
2

我生成了一个使用具有单个模块的开发工具的骨架应用程序。 我遇到的问题是,无论我在浏览器中输入哪个URL - 它总是返回apps/frontend/views/index.volt中的内容(前端是模块名称)。带模块的Phalcon 2.x骨架应用程序不会生成404未找到路径未找到

这里是我的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的配置

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

回答

1

的问题是在nginx的配置传递路线REQUEST_URI VS一特殊的_url变量。为了让多尔康工作,这个设置我不得不添加

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

之后

$router = new Router();

然后它开始工作,因为它应该:)

+0

请接受你的答案 – yergo

+0

完成: ) 对于那个很抱歉 – intellion