[英]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;
}
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;
}
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.