0

According to other asked questions like this one, I did many doings to prevent this request expired message but there is no solution for my issue.

In the long run I recognized that the message appears when I call a service method inside a controller which run on form action!

Here is my codes samples with some descriptions:

My route:

Route::post('Material/{id}', 'MaterialController@updateMaterial')->name('updateMaterial');

Material Controller Constructor:

public function __construct(CustomService $srv)
{
    $this->middleware('admin')->only(['updateMaterial']);

    $this->srv= $srv;
}

srv is a protected attribute in MaterialController class.

updateMaterial Method:

 public function updateMaterial($id,Request $request)
{
    $this->validate($request, [...]);

    $material = $this->srv->updateMaterial($request, $id);

    if ($material)
        return view('panel._materials.edit-material')
            ->with('material', $material)
            ->with('success', 1);

}

I also have a provider for CustomService with name CustomServiceProvider and here is the register method of the provider:

public function register()
{
    $this->app->bind(CustomService::class,function($app){
        return new CustomService();
    });
}

and I registered it as a provider in config/app.php.

So when I return something before calling service updateMaterial method, it's OK. but when the method runs, the issue appears!

I don'n have any idea about!

Update: And here is updateMaterial of CustomService:

 public function updateMaterial($request, $id)
{
    $material = Material::find($id);
    if (!$material)
        return false;

    if ($request->has('unit'))
        $material->unit = $request['unit'];
    if ($request->has('price'))
        $material->price = $request['price'];
    if ($request->has('type'))
        $material->type = $request['type'];
    if ($request->has('is_active'))
        $material->is_active = $request['is_active'];

    $material->updated_at = Carbon::now();

    $material->save();
    return $material;
}

I also create a new project with Laravel 5.5.0 and without adding any complexity I just added a post route and call it in form action, but nothing changed!

Behnam Azimi
  • 2,260
  • 3
  • 34
  • 52
  • where's `updateMaterial` method? where you call the function `updateMaterialPage`? – Ben Dec 18 '17 at 01:31
  • Im sorry. There was a mistake that I edit question. There is no updateMaterialPage. – Behnam Azimi Dec 18 '17 at 16:24
  • 1
    Possible duplicate of [Laravel 5.5 The page has expired due to inactivity error while sending form](https://stackoverflow.com/questions/46551940/laravel-5-5-the-page-has-expired-due-to-inactivity-error-while-sending-form) – Ben Dec 18 '17 at 16:37
  • But as I wrote, its ok when I do anything inside updateMaterial method but the issue appears when call a service method to doing sometjing. – Behnam Azimi Dec 18 '17 at 22:03
  • can you post the code method `CustomService::updateMaterial`? – Ben Dec 19 '17 at 00:51
  • Question updated, Your third comment solves my issue but not at all, I mean I must do that solution on every page reloading! – Behnam Azimi Dec 19 '17 at 07:10

1 Answers1

1

This is just an issue for Windows users on Local Environment. I suffered a lot with this also when on Windows. Once you deploy to your production server, you won't have any issue at all.

It's important to note that this is not an issue with Laravel 5.5 version only. I first saw this issue in version 5.2.

I think a good fix for this would maybe be using something like Homestead or Vessel from Fideloper. Honestly I only suffered this problem when using Windows.