【发布时间】:2014-05-01 14:03:55
【问题描述】:
虽然我已经成功地为包含 $Action/$ID/$OtherID 的典型模式设置了 AjaxController,但我似乎不知道如何为 $Action + 两个以上参数设置模式。
我正在尝试处理简单的“计算器”网址,如:myajax/add/5/6/7
routes.yml
Director:
rules:
'myajax//$action/$a/$b/$c': 'AjaxPage_Controller'
AjaxPage.php
<?php
class AjaxPage extends Page {
}
class AjaxPage_Controller extends Page_Controller {
public static $url_handlers = array(
'myajax/add/$a/$b/$c' => 'add',
);
private static $allowed_actions = array (
'add',
);
public function add($request){
$v1 = (int) $request->param('a');
$v2 = (int) $request->param('b');
$v3 = (int) $request->param('c');
echo json_encode(array('result' => $v1 + $v2 + $v3));
return;
}
}
然后,当我访问:myajax/add/5/6/7?debug_request=1 我看到 404 Page not found 带有以下调试信息:
Debug (line 250 of RequestHandler.php): Testing 'myajax/add/$a!/$b!/$c' with 'add/5/6/7' on AjaxPage_Controller
Debug (line 250 of RequestHandler.php): Testing '$Action//$ID/$OtherID' with 'add/5/6/7' on AjaxPage_Controller
Debug (line 258 of RequestHandler.php): Rule '$Action//$ID/$OtherID' matched to action 'handleAction' on AjaxPage_Controller. Latest request params: array ( 'Action' => 'add', 'ID' => '5', 'OtherID' => '6', )
{"result":18}
Debug (line 250 of RequestHandler.php): Testing '$Action//$ID/$OtherID' with '' on ErrorPage_Controller
Debug (line 258 of RequestHandler.php): Rule '$Action//$ID/$OtherID' matched to action 'handleAction' on ErrorPage_Controller. Latest request params: array ( 'Action' => NULL, 'ID' => NULL, 'OtherID' => NULL, )
Debug (line 184 of RequestHandler.php): Action not set; using default action method name 'index'
如您所见 - 在调试信息的中间有一个正确的结果被回显,尽管哪个框架仍在寻求回退并产生 404。
有谁知道这里发生了什么(即我在这里犯了什么样的错误)?我想我已经利用了模式中移位点// 的所有组合。每次尝试之后都是开发/构建和刷新
【问题讨论】:
标签: controller url-routing silverstripe