CodeIgniter Forums
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
namespace App\Controllers\Administration;

use 
App\Controllers\RootController;
use 
App\Models\Administration\UserModel;

class 
User extends RootController
{
    public function __construct() { parent::__construct(); }

    public function index() { ... show all users ...} // route for URI /administration/user/
    public function new() { ... }
    public function create() { ... }
    public function show(int $id) { ... }
    public function edit(int $id) { ... }
    public function update(int $id) { ... }
    public function delete(int $id) { ... }

The overview (index method), show user, edit, new,  methods all work except for the delete method.
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...  Sad
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)']);
// Roughly equivalent to the following
$routes->get('administration/user/new', 'User::new');
$routes->post('administration/user', 'User::create');
$routes->get('administration/user', 'User::index');
$routes->get('administration/user/(:num)', 'User::show/$1');
$routes->get('administration/user/(:num)/edit''User::edit/$1');
$routes->put('administration/user/(:num)''User::update/$1');
$routes->patch('administration/user/(:num)''User::update/$1');
$routes->delete('administration/user/(:num)''User::delete/$1'); 



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/1
Per the documentation, your resource declaration would do something like this:

PHP Code:
$routes->resource('administration/user', ['placeholder' => '(:num)']);
// Roughly equivalent to the following
$routes->get('administration/user/new', 'User::new');
$routes->post('administration/user', 'User::create');
$routes->get('administration/user', 'User::index');
$routes->get('administration/user/(:num)', 'User::show/$1');
$routes->get('administration/user/(:num)/edit''User::edit/$1');
$routes->put('administration/user/(:num)''User::update/$1');
$routes->patch('administration/user/(:num)''User::update/$1');
$routes->delete('administration/user/(:num)''User::delete/$1'); 
Hi 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)
axios.delete(url, config)
axios.post(url, data, config)
axios.put(url, data, config)
axios.patch(url, data, 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]);

// The following equivalent routes are created:
$routes->post('photos/(:segment)/delete''Photos::delete/$1');
$routes->post('photos/(:segment)',        'Photos::update/$1');