laravel get 和 post 路由的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29239881/
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
difference between laravel get and post route
提问by mirzap
I am a beginner in laravel i am shifting from codeigniter to laravel so i dont have the concepts of routes.Can any one tell me what is the difference between a post and get route in laravel 5.
我是 Laravel 的初学者,我正在从 codeigniter 转移到 Laravel,所以我没有路线的概念。谁能告诉我 Laravel 5 中的 post 和 get 路线有什么区别。
Basic GET Route
基本 GET 路由
Route::get('/', function()
{
return 'Hello World';
});
Basic POST Route
基本 POST 路由
Route::post('foo/bar', function()
{
return 'Hello World';
});
Is their any disadvantage or benefit or if i use both of them at same time And when should i use both of them what happen if i pass parameter to them when i am using them at the same time.
它们有什么缺点或好处吗,或者我是否同时使用它们?如果我同时使用它们时将参数传递给它们,会发生什么情况。
Route::match(['get', 'post'], '/', function()
{
return 'Hello World';
});
回答by mirzap
It's matter of HTTP protocol. Simply said, GET is usually used for presenting/viewing something, while POST is used to change something. For example, when you fetching data for some user you use GET method and it'll look something like this:
这是HTTP协议的问题。简单地说,GET 通常用于呈现/查看某些内容,而 POST 用于更改某些内容。例如,当您为某个用户获取数据时,您使用 GET 方法,它看起来像这样:
Route::get('users/{id}', function($id) {
$user = \App\User::find($id);
echo "Name: " . $user->name . '<br>';
echo "Email: " . $user->email;
});
while with POST method you create or update user data (when user submits form you send POST request to this route):
使用 POST 方法创建或更新用户数据(当用户提交表单时,您向此路由发送 POST 请求):
Route::post('users', function() {
try {
\App\User::create([
'name' => \Input::get('name'),
'email' => \Input::get('email'),
'password' => bcrypt(\Input::get('password'))
]);
return Redirect::intended('/');
} catch(Exception $e) {
return $e->getMessage();
}
});
It's just a simple example, but I hope you can see the difference.
这只是一个简单的例子,但我希望你能看到不同之处。