简体   繁体   English

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

[英]how to get url parameter in controller in 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当我点击添加到收藏夹时......我在 url 中得到了这个

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

web.php网站.php

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

how can i get the url pass name in controller.....我怎样才能在控制器中获取 url 传递名称.....

public function add(Request $request)
{
 
$request->get("") ////////////how can i get the string i passed on url 

}

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:add方法:

public function add(Request $request, $slug)

Then value of the $slug variable 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

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.我希望你会明白。

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!希望这会有所帮助!

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;

}
  1. If you have your parameter attached to the URL after the question mark like如果您在问号之后将参数附加到 URL,例如

http://www.siteurl.com/someRoute?key=value http://www.siteurl.com/someRoute?key=value

Your route for this controller will look something like this这个控制器的路线看起来像这样

public function controllerMethod(Request $request) {
$key = $request->key
echo $key;
}
  1. If you have your parameter attached to the URL without question mark like如果您将参数附加到没有问号的 URL,例如

http://www.siteurl.com/someRoute/value http://www.siteurl.com/someRoute/value

Your route for this controller will look something like this这个控制器的路线看起来像这样

Route::get('someRoute/{key}', $controller . 'controllerMethod');

You can get the value of this parameter in your controller function by passing the same name of variable in your controller method as you used in the route.您可以通过在控制器方法中传递与路由中使用的相同名称的变量来在控制器函数中获取此参数的值。

public function controllerMethod(Request $request, $key) {
echo $key;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM