I have an app with some routes, but I'm having an error when including a parameter
$route['contato'] = '/contato';
$route['contato/(:num)']['GET'] = '/contato/$1';
$route['contato/(:num)']['PUT'] = 'contato/update/$1';
$route['contato/(:num)']['DELETE'] = 'contato/delete/$1';
Controller
public function index()
{
$data = '';
$id = $this->input->get('id')
if (isset($id)) {
$data = $this->contato_model->get($id);
} else {
$data = $this->contato_model->get();
}
echo $this->ultils->toJson($data);
}
More when accessing the api https://localhost/api/contact/1
via url
Give error not found (404)
Return of just one record, if I change the code to ?=id and pass it in the url contato?id=2 it works, I need it to be that way in the /contato/2 route
in config/routes.php
you need to specify which function call within the class:
$route['contato'] = '/contato/index';
$route['contato/(:num)']['GET'] = '/contato/index/$1';
and the function should look like this:
public function index($id = false) {
$data = '';
if (is_numeric($id)) {
$data = $this->contato_model->get($id);
} else {
$data = $this->contato_model->get();
}
echo $this->ultils->toJson($data);
}
Passing URI Segments to your methods
If your URI contains more than two segments they will be passed to your method as parameters.
For example, let’s say you have a URI like this:
example.com/index.php/products/shoes/sandals/123
Your method will be passed URI segments 3 and 4 (“sandals” and “123”):
<?php
class Products extends CI_Controller {
public function shoes($category, $id) {
echo $category;
echo $id;
}
}
https://codeigniter.com/userguide3/general/controllers.html#passing-uri-segments-to-your-methods