mercredi 23 mai 2018

weird behavior using "redirect back" and 2 or more tabs with Laravel 5, only on Server

i have a big and weird problem using Laravel 5.4 on shared hosting.

When laravel use "redirect back", it redirect to the last visited page.

Let's imagine this:

We have a site with 2 sections: Products and Categories. We visit the products page and the url is site.com/products. This page have 3 buttons, new, edit and gallery.

We want to edit the producto with ID 3 (.../products/3/edit) so we edit its information and save it. No problems here, validations were oK.

BUT we need to edit again the product so we go to edit page (.../products/3/edit). The name field accepts max 10 letters but we typed 11. We open the categories page on a different tab (../categories). Then we press the "save" button but validation will fail because the name field have 11 letters.

And booom! Laravel redirect to the categories page because that was the last opened page but it should redirect us to the edit page.

This happens if i use a "form request validation" ( public function update(ExampleRequest $request){...} )

or if i use a simple request validation ( $request->validate(['title' => 'max:10, ...]); )

Both examples have the same problem.

In other hand, working with controllers, if i use return redirect()->back()... or return Redirect::back()..., the same behavior happens, it redirect to the last opened page.

So, When i'm creating, updating, deleting, etc, if the validation fails or if i use redirect()->back(), this will redirect me to the last opened page and if i opened a page in another tab, that page will be where the "redirect" will send me. If i use only 1 tab, everything works fine.

This happens with all my forms and only on the server, the local environment is ok.

Now, my code:

.htaccess

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    # To switch to PHP 7
    AddType application/x-httpd-php56 .php

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www.example.com [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

Controller

use App\Http\Requests\ProductRequest;

public function update(ProductRequest $request, $id)
{
  $product = Product::findOrFail($id);
  $product->update( $request->all() );

  $msgResponse = [...message...];

  return Redirect::to('/products')->with($msgResponse);
}

ProductRequest

<?php
namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class ProductRequest extends FormRequest{

  public function authorize(){
    return true;
  }

  public function rules()
  {
    return [
      'title' => 'required|max:10|...',
      'otherfield' => ['required', Rule::in([...]),],
      ...
    ];
  }
}

Site:

PHP 7.0, Shared server, Laravel 5.4

Please help! :(



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire