laravel - php redirect to another page in lavarel -
i have modal view (the 1 bootstrap) in front end.
upon clicking submit button user going function in controller:
route::post('post_question', array('uses' => 'questioncontroller@postquestion'));
and @ end of postquestion want redirect page.
i tried:
return redirect::to('mywebsite/question/1'); return response::make( '', 302 )->header( 'location', 'mywebsite/question/1' ); return redirect::route('question/'.$question_id)->with("question",question::find($question_id)); header("location: mywebsite/question/$question_id");
none seem work though.
the thing is, can see request in xhr page not redirected. modal somehow blocking behavior?
you can redirect ajax request. however, find results not quite expected.
on redirect, laravel should set response code header redirect response , content of redirected page sent.
you 1 of 2 things depending on how wanted handle things.
- send json response submitted form meta data parameter , use meta data in success function set
window.location
.
your laravel controller responding post bit this:
public function postquestion() { // stuff set $question return [ 'question' => $question, 'meta' => [ 'redirect_url' => url('mywebsite/question/'.$question->id), 'status' => '400', // other meta data may want send ], ]; }
then assuming doing jquery ajax call, change success callback (i'm calling questionsubmitsuccess
here):
questionsubmitsuccess = function (data) { // may want before redirecting user if (data.meta.redirect_url) { // redirects page window.location = data.meta.redirect_url; } }
- continue redirecting controller , bit more similar rails turbo links , replace entire page javascript:
you can few ways: using [modify url without reloading page browser history api), or using jquery.load submit form.
the browser history api might work bit easier still allow handle response errors, works in more modern browsers. jquery.load require rewriting bit of ajax submitting code , harder handle things errors (it replace page content no matter status code can tell), has better browser support.
imo, first approach bit more maningful api endpoint usable other single implementation. also, there fewer points of failure , error states manage compared trying replace entire dom without page reload.
Comments
Post a Comment