0
votes

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.

3
Do you have any other routes with /client prefix?Laerte
What happens when you switch the order of routes?Dino Numić
I tried and the order of routes doesn't seem to change result. Also, I have few others routes with /client prefix (and theirs form in html).Glastis
First, composer dump-autoload. Then, check if php artisan route:list show both paths.Jeff Harris
It seems that I have old routes that doesn't exists anymore. I believe it's definitely cached somewhere. EDIT: OH THANKS DUDE, I solved it with a php artisan route:cacheGlastis

3 Answers

1
votes

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.

0
votes

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>
0
votes

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.