![]() |
Problem with route on CodeIgniter API - 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: Problem with route on CodeIgniter API (/showthread.php?tid=92618) |
Problem with route on CodeIgniter API - wolverine4277 - 03-18-2025 Hi, I'm begining using APIs with CodeIgniter. After create a new project, the endpoint using POST doesn't works ( POST http://localhost/lab/php/client/ do de same that GET http://localhost/lab/php/client/, return http code 200 and all de clients ). Clearly I'm doing something wrong, but I don't realize the mistake I'm making. Someone with more experience than me (I recently started with the API theme with CodeIgniter4) could help me. Regards. Sebastián Step by step Create a lab folder inside www Inside the lab folder: Code: composer create-project codeigniter4/appstarter php Code: www Edit .env: Code: app.baseURL = 'http://localhost/lab/php/' In this point if I open http://localhost/lab/php/ I see the list of files, if I open http://localhost/lab/php/public/ I see the welcome page. After adding .htaccess file to project root with: Code: <IfModule mod_rewrite.c> Code: www When I open http://localhost/lab/php/ I see the welcome page. Now I add two controllers for do some testing (ClientController and ProductController) PHP Code: $routes->get('client', 'ClientController::index'); After this i check one by one with Postman. The only one that doesn't works was POST. POST http://localhost/lab/php/client/ do de same that GET http://localhost/lab/php/client/, return http code 200 and all de clients I know that i can do something like this: $routes->resource('client', ['controller' =>'ClientController']); but after the problem i separate the routes to check one by one RE: Problem with route on CodeIgniter API - wolverine4277 - 03-18-2025 Well, I answered myself... the problem (after much testing) was that I was calling http://localhost/lab/php/client/ (with the slash at the end) instead of calling http://localhost/lab/php/client. Correcting that works fine. Now I have a question, if I indicate POST it is correct to call GET, is it possible to avoid this behavior? Greetings. RE: Problem with route on CodeIgniter API - InsiteFX - 03-18-2025 You can supply multiple verbs that a route should match by passing them in as an array to the match() method: PHP Code: $routes->match(['GET', 'PUT'], 'products', 'Product::feature'); RE: Problem with route on CodeIgniter API - wolverine4277 - 03-19-2025 (03-18-2025, 10:40 PM)InsiteFX Wrote: You can supply multiple verbs that a route should match by passing them in as an array to the match() method: Hi InsiteFX, my question is Can I avoid that POST http://localhost/lab/php/client/ call GET http://localhost/lab/php/client? The slash produces the problem, without ir works fine (I added it by mistake in testing and I wanted to know if it is possible that it returns an error when placing it) Regards. RE: Problem with route on CodeIgniter API - InsiteFX - 03-20-2025 See this thread on it also. |