Problem with route on CodeIgniter API |
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
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.
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'); What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(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.
See this thread on it also.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |