Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: requests PUT type without segment redirects to index method instead update method #3343

Closed
reszko opened this issue Jul 17, 2020 · 3 comments

Comments

@reszko
Copy link

reszko commented Jul 17, 2020

I have created a restful resource to handle my api requests, like this:
$routes->resource('api/categoria', ['controller' => 'Api\Categoria']);

When I make a PUT request, like this:
PUT localhost:8080/api/categoria/5 it works fine, calling the desired method inside the Categoria controller, update, in this case

But, when I make a request without an ID, it redirects to the index method inside the Categoria controller
My intention is when the user doesn't send an ID I will respond him with a fail message telling that an ID is required, but it is not working like that.

How to reproduce the error:

  • Fresh installation of CodeIgniter appstarter

  • Create a restful resource, like my example

  • Create a Controller and two methods inside: index() and update()

  • Use Postman to simulate a put request with and without an ID

When you make the request with an ID, it will be redirected to the update method, like expected.
But, when the ID is removed, it redirects to the index() method.

The only way I find out to fix this is creating manually the route that handles the PUT request, like so:

  • $routes->put('api/categoria', 'Api\Categoria::update');

Environment: Localhost, windows 10
PHP 7.3.1

@reszko reszko added the bug Verified issues on the current code behavior or pull requests that will fix them label Jul 17, 2020
@reszko reszko changed the title Bug: request to put/(:segment) redirects to index method instead update method Bug: requests PUT type without segment redirects to index method instead update method Jul 17, 2020
@MGatner
Copy link
Member

MGatner commented Jul 17, 2020

This isn’t a big, it’s the predefined routes associated with a ResourceController. See https://codeigniter4.github.io/CodeIgniter4/incoming/restful.html#presenter-controller-comparison

What would you expect of a PUT to resource root?

@reszko
Copy link
Author

reszko commented Jul 18, 2020

I would expect an route error message, telling me that it is a not implemented route, instead showing all my records from the index method.

Am I correct to think like that?

Thank you.

@michalsn
Copy link
Member

@reszko Change auto route settings to $routes->setAutoRoute(false) in your app/Config/Routes.php file.

@michalsn michalsn removed the bug Verified issues on the current code behavior or pull requests that will fix them label Jul 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants