I'm trying to get first things first in Ajax in SS 3.1 with this rudimentary example of an Ajax calculator:
JS:
$('button').on('click', function(){
$.get('myajax/add/5/6', function(data){
alert(data.result);
})
})
AjaxPage.php:
<?php
class AjaxPage extends Page {
}
class AjaxPage_Controller extends Page_Controller {
private static $url_handlers = array(
'myajax/add//$num1/$num2' => 'add',
'myajax/multiply//$num1/$num2' => 'multiply',
);
private static $allowed_actions = array (
'add',
'multiply'
);
public function add(){
$v1 = (int) $this->getRequest()->param('num1');
$v2 = (int) $this->getRequest()->param('num2');
echo json_encode(array('result' => $v1 + $v2));
}
public function multiply(){
$v1 = (int) $this->getRequest()->param('num1');
$v2 = (int) $this->getRequest()->param('num2');
echo json_encode(array('result' => $v1 * $v2));
}
}
debug info
Debug (line 250 of RequestHandler.php): Testing 'myajax/add//$num1/$num2' with '' on AjaxPage_Controller
Debug (line 250 of RequestHandler.php): Testing 'myajax/multiply//$num1/$num2' with '' on AjaxPage_Controller
Debug (line 250 of RequestHandler.php): Testing '$Action//$ID/$OtherID' with '' 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' => NULL, 'ID' => NULL, 'OtherID' => NULL, )
Debug (line 184 of RequestHandler.php): Action not set; using default action method name 'index'
routes.yml
Director:
rules:
'myajax/add//$num1/$num2': 'AjaxPage_Controller'
'myajax/multiply//$num1/$num2': 'AjaxPage_Controller'
So, could anyone shed some light on me as per why the first two attempts keep failing and the default fallback comes into action?