Reputation: 127
I have a very simple web app created in Laravel 5.5:
There is a database with a list of coupon codes that have either not been redeemed or been redeemed. 0 is not redeemed and 1 is redeemed.
When someone enters a string into a HTML form input and submits it, Laravel goes to a route with that string as a variable.
The Controller code is as follows:
public function redeemCoupon ($coupon_code)
{
$coupon = Coupon::where('coupon_code', $coupon_code)->first();
if ($coupon === null) {
return view ('pages.no-coupon');
}
else if ($coupon->redeemed == 1) {
return view ('pages.used-coupon');
}
else {
$coupon->redeemed = 1;
$coupon->update();
return view('pages.redeemed-coupon', compact('coupon') );
}
}
Route:
Route::post('/redeem-coupon/{coupon_code}', 'CouponController@redeemCoupon');
You can try it out here: http://178.62.4.225
Everything works fine when done normally, tested on the code "code01". When I enter it and it hasn't been redeemed, it says so, and redeeming it changes the column in the database from 0 to 1. If I try the process again it tells me it has already been redeemed.
The issue is when I'm on the page that tells me it's been redeemed: http://178.62.4.225/redeem-coupon/code01
If I refresh it with CTRL + R, it just reloads and says it's already been redeemed. But if I paste the URL into a new tab or click into it and refresh by clicking enter, it gives " MethodNotAllowedHttpException" and the resulting debug screen, from what I can tell, offers nothing of use.
Help!
Upvotes: 0
Views: 123
Reputation: 333
Is redeemed set as protected? Also displaying app_debug true displays all your DB connection info (user and pass)
More than likely to be due to the _method.
What page is _method = "POST" on?
Upvotes: 0
Reputation: 127
Changing
Route::post('/redeem-coupon/{coupon_code}', 'CouponController@redeemCoupon');
to
Route::any('/redeem-coupon/{coupon_code}', 'CouponController@redeemCoupon');
Did the trick
Upvotes: 1
Reputation: 876
You are doing a GET request and define a post route change
Route::post('/redeem-coupon/{coupon_code}', 'CouponController@redeemCoupon');
to:
Route::get('/redeem-coupon/{coupon_code}', 'CouponController@redeemCoupon');
Upvotes: 0