在laravel中的同一个控制器内的两个动作上的post方法

时间:2017-12-20 06:34:10

标签: laravel laravel-5.5

以下是我的路线档案,即 web.php

Route::post('finddomainname','DomainController@finddomainname')->name('finddomainname');

Route::post('registerdomains','DomainController@registerdomains')->name('registerdomains');

以下是我的 DomainController 上的代码,用于所使用的操作,

public function finddomainname(Request $request)
{
    $this->validate($request,
        ['searchdomaintxt'=>'required',
        'searchdomainext'=>'required']);

    $searchdomaintxt    =  $request->input('searchdomaintxt');
    $searchdomainext    =  $request->input('searchdomainext');
    $domainname="";
    if($searchdomaintxt && $searchdomainext)
    {   
        foreach($searchdomainext as $ext)
        {
            $domainname.=$searchdomaintxt.".".$ext.",";
        }

        //dd($domainnames);
        $domainnames= rtrim($domainname,',');

        $response=$this->soap->multidomainsearch($domainnames);
        $result=$response['RESPONSE']['DOMAINSEARCH'];
        //dd($result);
        if($result){
            //return redirect()->action('searchresults', array('response' => $result));
            return view('domain.searchresults',['response'=>$result]);
        }
        else
        {
            return view('domain.searchresults',['response'=>'']);
        }


    }
}

以下是提交数据后控制的第二个动作

public function registerdomains(registerDomainsValidation $request)
{       

    $domains=$request->input('selecteddomains');
    $selectedyear =$request->input('selectedyear');
    $domaincontactid=\Session::get('domaincontactid');
    $alldomains='';
    foreach($domains as $domain)
    {
        $alldomains.=$domain.",";
    }
    $alldomains=rtrim($alldomains,',');
    $response=$this->soap->registerdomains($alldomains,$domaincontactid,$selectedyear);
    return view('domain.searchresults',['response'=>$response]);
}

但是当我提交数据时,它会显示此错误

    protected function methodNotAllowed(array $others)
{
    throw new MethodNotAllowedHttpException($others);
}

1 个答案:

答案 0 :(得分:0)

您正尝试使用POST请求访问GET路由,这就是您收到MethodNotAllowedHttpException的原因。要解决此问题,请确保您的<form></form>标记包含相应的method属性。

<form action="{{ YOUR_URL }}" method="POST">
    ...
</form>

或者,如果您要在控制器中执行 请求,可以使用Guzzle发送http请求。在您的控制器中,您可以:

use GuzzleHttp\Client;

$client = new Client();
$response = $client->request('POST', 'YOUR_URL', [
    'form_params' => [
        'foo' => 'bar'
    ]
]);