RESTful resource handling and delete: controller method not found - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: RESTful resource handling and delete: controller method not found (/showthread.php?tid=81054) |
RESTful resource handling and delete: controller method not found - Zeff - 01-19-2022 Hi all, I created a controller User in a subfolder ('administration'): App/Controllers/Administration/User.php Acording to https://codeigniter4.github.io/userguide/incoming/restful.html, I added a route (config\routes.php): Code: $routes->resource('administration/user', ['placeholder' => '(:num)']); Within my User controller, I created the corresponding methods: PHP Code: <?php URI requests to: - http://localhost/administration/user/1/delete or - http://localhost/administration/user/delete/1 result in a '404 - File Not Found | Controller method is not found: user' error So it seems that the URI is not correctly translated to the /administration/user controller; user is seen as method... Can anyone help me with this weird issue? Many thanks in advance! RE: RESTful resource handling and delete: controller method not found - xenomorph1030 - 01-19-2022 Are you using the proper HTTP verb when making the request? It looks like you need to use the DELETE verb to administration/user/1 Per the documentation, your resource declaration would do something like this: PHP Code: $routes->resource('administration/user', ['placeholder' => '(:num)']); RE: RESTful resource handling and delete: controller method not found - Zeff - 01-19-2022 (01-19-2022, 06:55 AM)xenomorph1030 Wrote: Are you using the proper HTTP verb when making the request? It looks like you need to use the DELETE verb to administration/user/1Hi xenomorph1030, Thanks for your reply. If I request administration/user/delete/<id>, it should be translated to the correct verb no? (Since I use $routes->resource()) Cheers, Zeff RE: RESTful resource handling and delete: controller method not found - xenomorph1030 - 01-19-2022 It depends on how you are requesting it. Anything visited in a browser is a GET request. HTML forms are usually POST (method="post"). The other HTTP verbs (PUT, PATCH, DELETE) have to be specified in whatever library or application you are using to call it. For example, if you are using Axios (common in VueJS), you can do something like: Code: axios.get(url, config) The advantage is you can use the same path (administration/user/1) for multiple operations depending on the HTTP verb. Per the resource you defined, there isn't suppose to be any administration/user/delete/1. RE: RESTful resource handling and delete: controller method not found - iRedds - 01-20-2022 You can use websafe parameter in resource method Resource routes PHP Code: $routes->resource('photos', ['websafe' => 1]); |