hello
i am new in cakephp and i am trying to develope an exercise about ajax call and datatables jquety plugin
- data are retrieved from a table called ousers
- clicking on a button fires the ajax request which should fill the table
this is the controller Ousers (omitting edit, delete view…)
<?php
namespace App\Controller;
use App\Controller\AppController;
class OusersController extends AppController
{
/**
* Index method
*
* @return void
*/
public function initialize() {
parent::initialize();
}
public function index()
{
//nothing to do...
}
public function getusers(){
$this->viewBuilder()->setTemplate('index');
$data=$this->request->getData(['Ousers.nome','Ousers.cognome','Ousers.email']);
$len=5;
$filtered=$len;
$draw=$this->request->getParam('draw');
$json_data=[
'draw'=>$draw,
'recordsTotal'=> 5,
'recordsFiltered'=> 5,
'data'=>$data
];
echo json_encode($json_data);
}
}
Following is the view (index.ctp)
<div>
<table id="users-table" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>nome</th>
<th>cognome</th>
<th>email</th>
</tr>
</thead>
</table>
</div>
<div class="upload-frm">
<input type="button" onclick="jsUsers();" value="click" />
</div>
and finally the js script
function jsUsers(){
$('#users-table').dataTable( {
"processing": true,
"serverSide": true,
"ajax":{
type: 'post',
url: "<?php echo Cake\Routing\Router::url(
array(
'controller'=>'Ousers',
'action'=>'getusers',
'_ext'=>'json',
'_full' => true //for full url path
));?>",
dataType: 'json',
error: function(e) {
alert("An error occurred: " + e.responseText.message);
console.log(e);
}
},"columns": [
{ "ousers": "id" },
{ "ousers": "nome" },
{ "ousers": "cognome" },
{ "ousers": "email" }
]
} );
}
i connect to http://localhost:8765/ousers/ then click on the button and i get a message like
"message": “Controller class Getusers could not be found.”,
** rl**": “/ousers/getusers.json”,**
so i think the problem is in the routing configuration for the resource:
Router::scope(’/ousers’, function ($routes) {
$routes->extensions([‘json’]);
$routes->resources(‘Ousers’);
$routes->fallbacks(‘DashedRoute’);
});
what is missing? thanks