Php 检查请求是否为GET或POST
在我的控制器/操作中:Php 检查请求是否为GET或POST,php,post,get,laravel-4,Php,Post,Get,Laravel 4,在我的控制器/操作中: if(!empty($_POST)) { if(Auth::attempt(Input::get('data'))) { return Redirect::intended(); } else { Session::flash('error_message',''); } } Laravel中是否有方法检查请求是否为POST或GET?$\u服务器['request\u method']用于此
if(!empty($_POST))
{
if(Auth::attempt(Input::get('data')))
{
return Redirect::intended();
}
else
{
Session::flash('error_message','');
}
}
Laravel
中是否有方法检查请求是否为POST
或GET
?$\u服务器['request\u method']
用于此目的
它将返回以下内容之一:
- “得到”
- “头”
- “职位”
- “放”
Request::getMethod()
获取用于当前请求的方法,但这应该很少需要,因为Laravel会根据请求类型调用控制器的正确方法(即getFoo()
用于get,postFoo()
用于POST)。当然有一种方法可以找到请求的类型,相反,您应该定义一个处理POST
请求的路由,因此不需要条件语句
routes.php
内部控制器/操作
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);
根据,有一个请求方法来检查它,因此您可以执行以下操作:
$method = Request::method();
或
上述解决方案已经过时 根据:
我已经在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;
}
}
Laravel 4使用camelCase时,Laravel 3用于
is_post
和is_get
不是吗?对于get和post使用单独的方法可能并不可取。对于简单的CRUD用例,对GET或POST使用相同的方法可以减少代码重复并降低开发的精神负荷better@Krynble你否决了我的答案,因为它是错误的,或者因为它不是最好的?只是因为我相信它没有回答问题;你说的有道理,但对于非常简单的任务(以及在添加服务器端验证时),我认为最好使用单控制器方法。@Krynble你如何使用单控制器方法Route::any()
?是的,我使用Route::any()并处理控制器方法中的所有内容;包括验证和在需要时显示键入的表单信息。Undefined variable:request
将请求传递给函数的情况下,此操作有效,例如function myFunction(request$request,$otherParams){if($request->method='get'){}
或仅使用request()->isMethod('post'))
anywhere导致函数request()
在Laravel中全局注册。
if (Request::isMethod('post'))
{
//
}
$method = $request->method();
if ($request->isMethod('post')) {
//
}
**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;
}
}