php 如何在 Laravel 中为相同模式路由 GET 和 POST?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18326030/
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
How to route GET and POST for same pattern in Laravel?
提问by enchance
Does anyone know of any way in Laravel 4 which combines these 2 lines into one?
有谁知道 Laravel 4 中将这两行合二为一的任何方式?
Route::get('login', 'AuthController@getLogin');
Route::post('login', 'AuthController@postLogin');
So instead of having to write both you only have to write one since their both using the 'same' method but also the URL remains as site.com/login
instead of a redirect to site.com/auth/login
?
因此,不必同时写两个,您只需要写一个,因为它们都使用“相同”的方法,而且 URL 保持不变,site.com/login
而不是重定向到site.com/auth/login
?
I'm curious since I remember CI has something like that where the URL remains the same and the controller is never shown:
我很好奇,因为我记得 CI 有类似的东西,其中 URL 保持不变并且控制器从未显示:
$route['(method1|method2)'] = 'controller/';
采纳答案by Mithredate
You could try the following:
您可以尝试以下操作:
Route::controller('login','AuthController');
Then in your AuthController class
implement these methods:
然后在你的AuthController class
实现这些方法中:
public function getIndex();
public function postIndex();
It should work ;)
它应该工作;)
回答by OrtegaGuillermo
The docs say...
文档说...
Route::match(array('GET', 'POST'), '/', function()
{
return 'Hello World';
});
source: http://laravel.com/docs/routing
回答by webnology
See the below code.
请参阅下面的代码。
Route::match(array('GET','POST'),'login', 'AuthController@login');
回答by Rubens Mariuzzo
You can combine all HTTP verbs for a route using:
您可以使用以下方法组合路由的所有 HTTP 动词:
Route::any('login', 'AuthController@login');
This will match both GET
and POST
HTTP verbs. And it will also match for PUT
, PATCH
& DELETE
.
这将匹配GET
和POST
HTTP 动词。它也将匹配PUT
, PATCH
& DELETE
。
回答by Sid
Route::any('login', 'AuthController@login');
and in controller:
并在控制器中:
if (Request::isMethod('post'))
{
// ... this is POST method
}
if (Request::isMethod('get'))
{
// ... this is GET method
}
...
回答by Rinto George
As per the latest docs, it should be
根据最新的文档,它应该是
Route::match(['get', 'post'], '/', function () {
//
});
回答by Igor Parra
Route::match(array('GET', 'POST', 'PUT'), "/", array(
'uses' => 'Controller@index',
'as' => 'index'
));
回答by Amir
In laravel 5.1 this can be achieved by Implicit Controllers. see what I found from the laravel documentation
在 laravel 5.1 中,这可以通过隐式控制器来实现。看看我从 laravel 文档中找到了什么
Route::controller('users', 'UserController');
Next, just add methods to your controller. The method names should begin with the HTTP verb they respond to followed by the title case version of the URI:
接下来,只需将方法添加到您的控制器。方法名称应以它们响应的 HTTP 动词开头,后跟 URI 的标题大小写版本:
<?php
namespace App\Http\Controllers;
class UserController extends Controller
{
/**
* Responds to requests to GET /users
*/
public function getIndex()
{
//
}
/**
* Responds to requests to GET /users/show/1
*/
public function getShow($id)
{
//
}
/**
* Responds to requests to GET /users/admin-profile
*/
public function getAdminProfile()
{
//
}
/**
* Responds to requests to POST /users/profile
*/
public function postProfile()
{
//
}
}
回答by Soubhagya Kumar Barik
In Routes
在路线中
Route::match(array('GET','POST'),'/login', 'AuthController@getLogin');
In Controller
在控制器中
public function login(Request $request){
$input = $request->all();
if($input){
//Do with your post parameters
}
return view('login');
}
回答by Mike Rockétt
Right, I'm answering using my mobile, and so I haven't tested this (if I remember correctly, it isn't in the documentation either). Here goes:
是的,我是用手机回答的,所以我还没有测试过这个(如果我没记错的话,它也没有在文档中)。开始:
Route::match('(GET|POST)', 'login',
'AuthController@login'
);
That should do the trick. If it doesn't, then Taylor had it removed from the core; which would then mean that nobody was using it.
这应该够了吧。如果没有,则泰勒将其从核心中移除;这将意味着没有人在使用它。