简体   繁体   中英

What is the easiest way to pass a variable from Route to Controller in laravel 5.4?

This might be super simple but I'm asking because I'm new to this.

I have a View named 'defaultpage' , I have two routes and one controller as below.

Route::get('about', 'PageController@showPage');

Route::get('welcome', 'PageController@showPage');

.

class PageController extends Controller
{
    public  function showPage(){

        return view('defaultpage', compact('var'));
    }
}

I want to pass a variable named $var from above routes to below controller , If $var is coming from 'about' route , $var should be equal to a value given in about route and the controller will pass it to the view , and same with the 'welcome' route.

How can I do this . Any help will be appreciated . Thank you .

Easiest way to pass variable from route to controller if the varible is coming from view is to put it a link as below

<a href="/myRoute/{{ $variableName }}">Click me</a>

In routes:

Route::get('myRoute/{variable}', 'ControllerName@getFunction');

In Controller

public function getFunction($variable){
  echo $variable;
}

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