CodeIgniter Forums
trailing slash misdirects resource controller - 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: trailing slash misdirects resource controller (/showthread.php?tid=79693)



trailing slash misdirects resource controller - lukmim - 07-20-2021

when specifying a resource route according to the user guide:
PHP Code:
$routes->resource('photos'); 
the create() controller method is correctly called when POSTing data to /photos, however when a trailing slash is added /photos/ the index() controller method is called instead


adding a route like
PHP Code:
$routes->post('users/''Users::create');
$routes->resource('users'); 
before the resource route doesn't help


RE: trailing slash misdirects resource controller - paulbalandan - 07-20-2021

This is because a URL ending with a trailing slash is treated as a directory, not a file.

This applies to all URLs in general, not only in API or CodeIgniter as a whole.

https://en.ryte.com/wiki/Trailing_Slashes#:~:text=A%20trailing%20slash%20is%20the,generally%20points%20to%20a%20file.


RE: trailing slash misdirects resource controller - lukmim - 07-21-2021

sorry, I don't see how it makes a POST request to /photos/ equivalent to a GET request to /photos in a REST resource. Seems like now I need to manually add a check if index() has been called with the incorrect method and return 404