使用 Laravel 传递 URL 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11144496/
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
URL Parameter Passing with Laravel
提问by Akash
I want to visit a page like...
我想访问一个页面,例如...
http://mysitelocaltion/user_name/user_id
This is just a virtual link, I have used a .htaccess -rewrite rule to internally pass "user_name" and "use_id" as get parameters for my actual page.
这只是一个虚拟链接,我使用了 .htaccess -rewrite 规则在内部传递“user_name”和“use_id”作为我实际页面的获取参数。
How do I achieve the same in Laravel?
我如何在 Laravel 中实现相同的目标?
Update:This shall help (documentation)
更新:这将有所帮助(文档)
Route::get('user/(:any)/task/(:num)', function ($username, $task_number) {
// $username will be replaced by the value of (:any)
// $task_number will be replaced by the integer in place of (:num)
$data = array(
'username' => $username,
'task' => $task_number
);
return View::make('tasks.for_user', $data);
});
回答by daylerees
Route::get('(:any)/(:any)', function($user_name, $user_id) {
echo $user_name;
});
Great to see you using Laravel!
很高兴看到你使用 Laravel!
回答by NIKHIL NEDIYODATH
You can add the following in your route
您可以在路线中添加以下内容
Route::get('user_name/{user_id}', 'YourControllerName@method_name');
In your controller you can access the value as follows
在您的控制器中,您可以按如下方式访问该值
public function method_name(Request $request, $user_id){
echo $user_id;
$user = User::find($user_id);
return view('view_name')->with('user', $user);
}