区分Laravel控制器中的GET和POST方法

区分Laravel控制器中的GET和POST方法

问题描述:

I have 2 routes like below,

Route::get('/','MainController@Date');
Route::post('/','MainController@Date');

or

Route::any('/','MainController@Date');

When get request is called i will calculate dates and hen post request is called i will get dates from form inputs.

when post method is called in my controler

$date1 = $request->get ( 'date1' );
$date2 = $request->get ( 'date2' );

when get is called

$date1 = will calculate using date function
$date2 = will calculate using date function

How differentiate both methods get and post, if get i should one set of things and for post another set of things

我有2条路线如下, p>

  Route :: 得到( '/', 'MainController @日期'); 
Route ::交( '/', 'MainController @日期'); 
 代码>  PRE> 
 
 

或 p>

  Route :: any('/','MainController @ Date'); 
  code>  pre> 
 
 

调用get请求时 我将计算日期和母鸡后请求被调用我将从表单输入中获取日期。 p>

在我的控制器中调用post方法 p>

  $ date1 = $ request-> get('date1'); 
 $ date2 = $ request-> get('date2'); 
  code>  pre> 
 
 

当get被调用时 p>

  $ date1 =将使用日期函数计算
 $ date2 =将使用日期函数计算
  code>  pre> 
 
  

如何获取和发布两种方法,如果我得到一组东西并发布另一组东西 p> div>

    <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class SmeController extends Controller
{
    /**
     * Do somthing
     * @param  Request  $request
     */
    public function update(Request $request)
    {
        if ($request->isMethod('post')) {
    //
        }
        if ($request->isMethod('get')) {
    //
        }
    }
}

you could also use $method = $request->method();

You can simply do this using below code

public function someMethod(Request $request)
{
        $method = $request->method();

        // to check if its a post method
        if ($request->isMethod('post')) {
            //
        }

        // to check if its a get method
        if ($request->isMethod('get')) {
            //
        }
}

The method method() will return the HTTP verb for the request. You may also use the isMethod method to verify that the HTTP verb matches a given string:

$request->query();//return only GET param
$request->request->all()// POST param
$request->input();//all