雖然我已經成功地爲包含$ Action/$ ID/$ OtherID的典型模式設置了一個AjaxController,但我似乎不知道如何爲$ Action設置一個模式+超過兩個參數。SilverStripe路由產生奇怪的結果與3個或更多的參數
我試圖處理簡單的 「計算器」 的網址爲: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
有誰知道什麼是在這裏發生(即。我在這裏犯了什麼樣的錯誤)?我認爲我已經在模式中利用了轉換點//
的所有組合。每個嘗試後跟dev/build和flush
精彩!就是這樣。非常感謝,尤其是。爲解釋。 – user776686