[英]Check if request is GET or POST
在我的控制器/動作中:
if(!empty($_POST))
{
if(Auth::attempt(Input::get('data')))
{
return Redirect::intended();
}
else
{
Session::flash('error_message','');
}
}
Laravel
沒有檢查請求是POST
還是GET
?
根據Laravels docs ,有一個 Request 方法來檢查它,所以你可以這樣做:
$method = Request::method();
或者
if (Request::isMethod('post'))
{
//
}
我已經在 laravel 版本中解決了我的問題,如下所示:7+
**In routes/web.php:**
Route::post('url', YourController@yourMethod);
**In app/Http/Controllers:**
public function yourMethod(Request $request) {
switch ($request->method()) {
case 'POST':
// do anything in 'post request';
break;
case 'GET':
// do anything in 'get request';
break;
default:
// invalid request
break;
}
}
當然有一種方法可以找出請求的類型,但是您應該定義一個處理POST
請求的路由,因此您不需要條件語句。
路由文件
Route::post('url', YourController@yourPostMethod);
在你的控制器/動作里面
if(Auth::attempt(Input::get('data')))
{
return Redirect::intended();
}
//You don't need else since you return.
Session::flash('error_message','');
GET
請求也是如此。
Route::get('url', YourController@yourGetMethod);
$_SERVER['REQUEST_METHOD']
用於此目的。
它返回以下之一:
使用Request::getMethod()
獲取用於當前請求的方法,但這應該很少需要,因為 Laravel 會根據請求類型調用控制器的正確方法(即getFoo()
用於 GET, postFoo()
用於 POST) .
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.