I am working on a Laravel 5 RESTful API that seems not to be routing the POST requests correctly.
This is my routes.php:
Route::group(array('prefix' => 'api/v1'), function()
{
Route::resource('messages', 'IncomingMessages');
});
And this is my controller:
class IncomingMessages extends Controller
{
public function index() {
return "This is index";
}
public function store() {
return "This is store";
}
public function update() {
return "This is update";
}
}
And this is what happens:
GET mydomain.com/api/v1/messages/
--> This is indexPUT mydomain.com/api/v1/messages/1
--> This is updatePOST mydomain.com/api/v1/messages/
--> This is index
This is what php artisan route:list
returns:
So, my question is:
What am I missing? Why is it routing to index()
instead of routing to store()
?
NOTES:
Kernel.php
UPDATE:
The problem was adding a trailing /
to the URL. So, instead of using this URL:
mydomain.com/api/v1/messages/
I tried with this one:
mydomain.com/api/v1/messages
and it worked
The problem was caused by a trailing /
being added to the URL. So, instead of using this URL:
mydomain.com/api/v1/messages/
I tried with this one:
mydomain.com/api/v1/messages
and it worked.
I discovered it by taking a look at the server's log. That is how I discovered that POST requests to the URL messages/
were redirected.
if you are still having this problem and the route is using FormRequest
check if there a authorize
function in it because it will redirect if it returns false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With