senty senty - 30 days ago 6
PHP Question

Redirect as POST from one controller to another

I have an array in my controller, and I want to redirect to another route with this array data.

class HomeController {
public function start() {
$dirs = ['X', 'Y'];

return redirect()->route('success', $dirs);
}

public function next($dirs) {
dd($dirs); // aim is getting data here
}
}


And my routes look like:

Route::post('/success', ['as' => 'success', 'uses' => 'HomeController@next']);


I tried get method but then I need to mess up the url and put a
/success/{dirs}
there, which I don't want.

What is the proper way of achieving this?

Answer Source

Use with method to put data into session and then retrieve it using session helper.

Try this

Route

Route::get('/success', 'HomeController@next')->name('success');

Controller

class HomeController {
    public function start() {
        $dirs = ['X', 'Y'];

        return redirect()->route('success')->with('dirs', $dirs);
    }

    public function next() {
        $dirs = session('dirs');
    }
}