Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get retuned value from one laravel route into another route

How to get value of one route into another

Route::get('/first_url', function () {
   return "Hello this is test";
});

I Tried something like this but not worked.

Route::get('/second_url', function () {
   $other_view = Redirect::to('first_url');
});

I want to get returned value from first_url to variable $other_view in second_url to process and manipulate returned value.

Using Redirect is changing url. Which I dont want to use.

Any Idea ??? Or Am I trying wrong thing to do.

like image 527
Drone Avatar asked Dec 05 '22 01:12

Drone


2 Answers

If you just want to return first_url, do this:

Route::get('/first_url', ['as' => 'firstRoute', function () {
    return "Hello this is test";
}]);

Route::get('/second_url', function () {
    return redirect()->route('firstRoute');
});

Learn more about redirects to routes here.

Update:

If you want to pass variable, you can use form or just create a link with parameters. You can try something like this {{ route('second_url', ['param' => 1]) }}

Then your second route will look like this:

Route::get('/second_url/{param}', ['uses' => 'MyController@myMethod', 'param' => 'param']);

And myMethod method in MyController:

public function myMethod($param){
    echo $param;
...
like image 95
Alexey Mezenin Avatar answered Dec 06 '22 15:12

Alexey Mezenin


I don't know why you would like to do this, but you can get the rendered contents of the route by executing a simple HTTP request to your route and reading the contents:

Route::get('/second_url', function () {
   $other_view = file_get_contents(URL::to('first_url'));

   return $other_view; // Outputs whatever 'first_url' renders
});
like image 31
tommy Avatar answered Dec 06 '22 15:12

tommy