laravel 如何在laravel的控制器中获取url参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47863575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:07:35  来源:igfitidea点击:

how to get url parameter in controller in laravel?

phplaravel

提问by Mirthful Nahid

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

当我点击添加到收藏时....我在 url 中得到这个

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

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

///web.php

///web.php

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

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

////controller ......我怎样才能在控制器中获取 url pass 名称.....

public function add(Request $request)
{

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

}

回答by Alexey Mezenin

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

您需要将参数添加到路由中。所以,它应该是这样的:

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

And the addmethod:

add方法:

public function add(Request $request, $slug)

Then value of the $slugvariable will be Chilli Green Salad

那么$slug变量的值将是Chilli Green Salad

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

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

回答by madalinivascu

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;

}

回答by Sagar Gautam

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.

我希望你会明白。

回答by adeltahir

In your router.php:

在您的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!

希望这会有所帮助!