0
votes



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,

2
Are you sure the problem is not in some javascript in the view? - Amarnasan
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 en.wikipedia.org/wiki/HTTP_301) - Amarnasan
@Amarnasan, Thank you for your answer, Here problem is I don't understand status 301 deeply - Tho Bui Ngoc

2 Answers

1
votes

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.

1
votes

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.