简体   繁体   中英

Laravel Route Redirect with URL Parameter

I am trying to generate a unique id and then redirect to a different route with id in the url parameter. But I am getting error as :

"Route [sequences] not defined."

Here is my route defined:

Route::get('/sequences_create','SequencesController@create');
Route::get('/sequences/{id}', 'SequencesController@show');

Here is the create function on the sequences controller:

public function create()
    {
        $uniq = 'seq'. uniqid();
        $seq = new Sequences;
        $seq->id = $uniq;
        $seq->user_id = auth()->user()->id;
        $seq->name = 'New Sequence'; //temp name
        $seq->save();
        return redirect()->route('sequences', ['id' => $uniq]);
    }

you need to name your route.

Route::get('/sequences_create','SequencesController@create')->name('sequences.create');
Route::get('/sequences/{id}', 'SequencesController@show')->name('sequences.show');

Then change your redirect to:

return redirect()->route('sequences.show', ['id' => $uniq]);

you have not set a name for your route by name() method.

try it:

Route::get('/sequences_create','SequencesController@create')->name('sequances.create');
Route::get('/sequences/{id}', 'SequencesController@show')->name('sequances.show');

then:

public function create()
    {
       //...
        return redirect()->route('sequences.show', ['id' => $uniq]);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM