Reputation: 179
For the first time I have a problem with a route in laravel 5 (5.8.35).
I have a form that makes a post request to /client/ban
<div class="text-center">
<form action="/client/ban" method="post">
{{ csrf_field() }}
Token:<br>
<label>
<input type="text" name="token">
</label>
<button type="submit" name="ban" value="1">Ban</button>
<button type="submit" name="ban" value="0">Pardon</button>
</form>
</div>
My routes are here, it should redirect my /client/ban
request to the action_ban_req
method.
Route::post('/client/ban', 'HomeController@action_ban_req');
Route::post('/client/new', 'HomeController@action_new_req');
Here are my two methods, in the HomeController.php
file, with, for debug purpose, have a very simple content (it contains other methods that works on theirs own routes);
public function action_ban_req(Request $request)
{
return "Test BAN";
}
public function action_new_req(Request $request)
{
return "Test NEW";
}
However, every time I make a request, parameters are sent to /client/ban
but it returns "Test NEW". I also tried with this route instead:
Route::post('/client/ban', function () { return 'Test'; });
Even with that, there is no difference, I'm still stuck with a "Test NEW" response.
Did I miss something?
Edit: Nothing change when I swap order of routes, my others routes with /client
prefix (that have other forms) works fine.
Upvotes: 0
Views: 3811
Reputation: 179
Thanks to Jeff Harris, I found the problem.
As he said, I executed:
composer dump-autoload && php artisan route:list
I got a few old routes that shouldn't exist anymore, and some routes that were redirecting to the wrong controllers, then the problem was that laravel wasn't updating its route cache even if the routes/web.php
file was edited, I don't know why.
I had to clear cache with the following:
php artisan route:cache
And after that php artisan route:list
listed routes that redirected to the rights methods.
Upvotes: 0
Reputation: 1
Glastis.
I think you cannot send multiple actions with same form. Web.php takes the first match. But you can check into the controller the button hitted.
public function ban(Request $request){
switch ($request->input('action')) {
case '0':
// Case bam
break;
case '1':
// Case Pardon
break;
}}
<button type="submit" name="action" value="0">Ban</button>
<button type="submit" name="action" value="1">Pardon</button>
Upvotes: 0
Reputation: 8178
Not sure how far along you are in your project, but a failed route may be defaulting to action_new_req
. Or, there may be another route catching what your are sending to post. Depending on how your site is set up, it looks like the problem may be that you are not sending a proper URL to the POST method.
Give this a try:
<form action="{{url('/client/ban')}}" method="post">
If the route wasn't getting the right base (e.g. http://yoursite/client/ban
), this should solve it.
Upvotes: 1