how to get url parameter in controller in laravel?

时间:2017-12-18 07:06:46

标签: php laravel

my view blade .....

  tableHtml += "<td><a href= '{{url('/add')}}/" + data.hits[i].recipe.label + "'> add to favotite</a></td>";

when i click add to fav....i get this in url

http://localhost/lily/public/add/Chilli%20Green%20Salad

///web.php

Route::get('/add', 'HomeController@add');

////controller ......how can i get the url pass name in controller .....

public function add(Request $request)
{

$request->get("") ////////////how can i get the string i passed on url 

}

4 个答案:

答案 0 :(得分:9)

You need to add the parameter to the route. So, it should look like this:

Route::get('add/{slug}', 'HomeController@add');

And the add method:

public function add(Request $request, $slug)

Then value of the $slug variable will be Chilli Green Salad

https://laravel.com/docs/5.5/routing#required-parameters

答案 1 :(得分:1)

Alter your url,add a get variabile

tableHtml += "<td><a href= '{{url('/add')}}/?slug=" + data.hits[i].recipe.label + "'> add to favotite</a></td>";

in your controller you use

public function add(Request $request)
{

echo $request->slug;

}

答案 2 :(得分:0)

In your router.php:

Route::get('/add/{recipe}', 'HomeController@add'); // if recipe is required
Route::get('/add/{recipe?}', 'HomeController@add'); // if recipe is optional

In your `controller:

public function add(Request $request, $recipe) {
  // play with $recipe
}

Hope this will help!

答案 3 :(得分:0)

You can do it like,

Route

Route::get('add/{data}', 'HomeController@add');

Controller

public function add(Request $request){
    // Access data variable like $request->data
}

I hope you will understand.