0



Please help me, I don't know why Laravel method is called twice times when I use command this command to redirect to new page:

Route:

Route::post('/editor/create/{productCode}', 'EditorController@create')->name('create-new-design');
Route::get('/editor/{designCode}', 'EditorController@edit')->name('edit-design');


EditorController:

public function create($productCode) {
    // .. do some thing & redirect to editor page
    return redirect()->route('edit-design', ['designCode' => $newDesignCode], 301);
}

public function edit($designCode){
    // this method is called twice

    $design = Design::where('code', '=', $designCode)->first();

    // do extra options --> return editor edit view
    return view('editor.edit');
}


Flow:
User request to create new Design by call action [POST]: /editor/create/{productCode} --> Server process & create Design Record then redirect user to editor page ( --> /editor/{designCode} ).


Question:
Why function public function edit($designCode) is call twice when user is redirected to edit page (or reload this page after create new design ) ?


Notes:
This project, I'm using:

  1. Apache server
  2. Laravel 5.8.*


Thank you,

Tho Bui Ngoc
  • 763
  • 1
  • 11
  • 36
  • Are you sure the problem is not in some javascript in the view? – Amarnasan May 14 '19 at 10:32
  • 1
    Why are you using that 301 code? It is only intended to use to fix old routes (like when you update your links to change to https form http https://en.wikipedia.org/wiki/HTTP_301) – Amarnasan May 14 '19 at 10:35
  • @Amarnasan, Thank you for your answer, Here problem is I don't understand status 301 deeply – Tho Bui Ngoc May 14 '19 at 11:12

2 Answers2

1

Try like this:

return redirect()->route('edit-design', $newDesignCode);

After each HTTP request, the page needs a refresh. It's the standard procedure. If you don't want to refresh the page, you can use AJAX calls and manage events with Javascript.

Kévin Bibollet
  • 3,573
  • 1
  • 15
  • 33
Diego Cespedes
  • 1,353
  • 4
  • 26
  • 47
1

try this

return redirect()->route('edit-design', ['designCode' => $newDesignCode]);

No Need To Pass the 301 again. I believe that might be the cause. Or You can share your edit method. It might contain policies/permission look ups that is causing the double execution.

Noob Coder
  • 2,816
  • 8
  • 36
  • 61