我有一个网站我试图使用两个模块。默认& API。该网站运行默认设置并正常工作(http://localhost)。但我正在尝试使用像这样的api模块(http://localhost/api/other-params)。但它仍然加载网站。我有两条路线设置如下:
'apiDivisionStandings' => array(
'type' => 'Zend_Controller_Router_Route',
'route' => 'api/division/standings/:userID',
'defaults' => array(
'module' => 'api',
'controller' => 'division',
'action' => 'standings'
)
),
'defaultSchoolSize' => array(
'type' => 'Zend_Controller_Router_Route',
'route' => ':state/:size/schools',
'defaults' => array(
'module' => 'default',
'controller' => 'school',
'action' => 'size'
)
),
在我的bootstrap文件中:
Zend_Date::setOptions(array('format_type' => 'php'));
Zend_Layout::startMvc(array('layout' => 'website/default' , 'layoutPath' => APPLICATION_PATH . '/default/layouts' , 'contentKey' => 'content'));
$routes = new Zend_Config(Routes::getRoutes());
$router = new Zend_Controller_Router_Rewrite();
$router->addConfig($routes, 'routes');
$this->bootstrap('frontController');
$frontController = Zend_Controller_Front::getInstance();
$frontController->setRouter($router);
$cOpts = array('lifetime' => 99000 ,'automatic_serialization' => true);
Zend_Registry::set('cache', Zend_Cache::factory('Core', 'File', $cOpts, array('cache_dir' => '../cache/')));
在我的index.php之前,我在bootstrap() - > run();
之前require_once '../library/Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->setFallbackAutoloader(true);
$loader->suppressNotFoundWarnings(false);
$loader->registerNamespace('Api_');
我的application.ini:
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.controllerDirectory = APPLICATION_PATH "/modules/default/controllers"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
最后,我的DivisionController在application / modules / api / controllers文件夹中的一部分(网站内容在application / modules / default中;工作正常):
class Api_DivisionController extends Zend_Rest_Controller
{
/**
* - Class setup
* - Tells framework not to render to view
* - sets default data type to JSON
*/
public function init()
{
$this->_helper->viewRenderer->setNoRender(true);
$this->_helper->contextSwitch()
->addActionContext('standings', array('xml','json'))
->addActionContext('news', array('xml', 'json'))
->setAutoJsonSerialization(true)
->initContext();
}
但是我没有在页面上获取JSON响应,而是在加载网页时收到错误消息,指出它无法找到视图。我错过了什么?
答案 0 :(得分:2)
ZF以相反的顺序匹配路由,所以我的猜测是它与你的默认路由匹配http://localhost/api/params,因为它首先检查它,并且路由器无法知道'api'不是有效的:状态。因此,尝试切换路由的顺序,以便API 1是最后一个(因此首先检查)。
如果这不起作用,请尝试暂时删除默认路由,然后查看API请求是否有效。如果他们不这样做,那么这表明您的API路线本身存在问题。