Why I need to put /1 in front of the url for put (update) in codeigniter 4 version 4.2.6 ?
routes.php :
$routes->resource('ApiManageProfile', ['controller' =>'App\Controllers\ApiData\ApiManageProfile']); // get, put, create, delete
ApiManageProfile.php
<?php
namespace App\Controllers\ApiData;
use App\Controllers\BaseController;
use CodeIgniter\RESTful\ResourceController;
use Codeigniter\API\ResponseTrait;
class ApiManageProfile extends ResourceController
{
use ResponseTrait;
function __construct()
{
}
// equal to get
public function index()
{
}
// equal to post
public function create() {
}
// equal to get
public function show($id = null) {
}
// equal to put
public function update($id = null) {
$id = $this->request->getVar('id');
$birthday = $this->request->getVar('birthday');
$phonenumber = $this->request->getVar('phonenumber');
echo "TESTING";
}
// equal to delete
public function delete($id = null) {
}
}
Then I use postman to call put with /1 :
https://testing.id/index.php/ApiManageProfile/1?id=71&phonenumber=1122211&birthday=2023-01-20
The code run correctly.
But if I use postman to call put without /1 :
https://testing.id/index.php/ApiManageProfile?id=71&phonenumber=1122211&birthday=2023-01-20
Then I got this error :
"title": "CodeIgniter\\Exceptions\\PageNotFoundException",
"type": "CodeIgniter\\Exceptions\\PageNotFoundException",
"code": 404,
"message": "Can't find a route for 'put: ApiManageProfile'.",
For the previous version codeigniter 4 Version 4.1.2 it is working without a problem
I cannot change all my Rest API to use /1 in front of the url for put because my Application is already launch. If I change the code in react native it will need a time to update the application. And people cannot update the data.
Codeigniter 4 seem change something in newest update version 4.2.6. Causing my routes broken in the application.
Seriously need help for this. What I can do ?
CodePudding user response:
$routes->resource('ApiManageProfile', ['controller' =>'\App\Controllers\ApiData\ApiManageProfile']); // get, put, create, delete
generates RESTFUL routes including the following for the PUT
action.
$routes->put('ApiManageProfile/(:segment)', '\App\Controllers\ApiData\ApiManageProfile::update/$1');
The segment isn't optional.
If you would like to implement the segment to be optional, exclude it from the generated routes and declare it explicitly that way.
$routes->resource(
'ApiManageProfile',
['controller' =>'\App\Controllers\ApiData\ApiManageProfile', 'except' => 'update']
); // get, create, delete
$routes->put(
'ApiManageProfile',
'\App\Controllers\ApiData\ApiManageProfile::update'
);