Welcome Guest, Not a member yet? Register   Sign In
Problem with route on CodeIgniter API
#1

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
I get a structure like that:

Code:
www
└── lab/
    └── php/ (project folder)
        └── public/

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>
    RewriteEngine On
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
<FilesMatch "^\.">
    Require all denied
</FilesMatch>


Code:
www
└── lab/
    └── php/ (project folder)
        ├── .htaccess
        └── public/

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');
$routes->get('client/(:segment)''ClientController::show/$1');
$routes->post('client''ClientController::create'); 
$routes->put('client/(:segment)''ClientController::update/$1');
$routes->patch('client/(:segment)''ClientController::update/$1');
$routes->delete('client/(:segment)''ClientController::delete/$1'); 

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
Reply
#2

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.
Reply
#3

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');

If 
that is what you mean
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(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:

PHP Code:
$routes->match(['GET''PUT'], 'products''Product::feature');

If 
that is what you mean

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.
Reply
#5

See this thread on it also.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB