Laravel Throwing MethodNotAllowedHttpException错误

时间:2017-03-17 13:09:50

标签: laravel

当我点击发送电子邮件并发送短信按钮时,我面临的客户页面的我的Routes.php文件

  Route::get('/customer', 'CustomerController@showCustomers');
Route::get('/customer/add', 'CustomerController@addNewCustomer');
Route::post('/customer/add', 'CustomerController@insertCustomer');
Route::get('/customer/delete/{id}', 'CustomerController@deleteCustomer');
Route::get('/customer/edit', 'CustomerController@editCustomer');
Route::post('/customer/edit', 'CustomerController@updateCustomer');
Route::get('/search-customers', 'CustomerController@searchCustomers');
Route::post('/customer/set_type', 'CustomerController@setType');
Route::get('/customer/send_sms', 'CustomerController@sendNotification');
Route::get('/customer/send_email', 'CustomerController@sendEmail');

Customer.blade文件

    <?php
include(app_path().'/includes/reports.php');
?>
@extends('layouts.dashboard')
@section('page_heading','Customers')
@section('section')
<style type="text/css">
    #customer_table_filter {
        display: none;
    }
</style>

<div class="col-sm-12">
    <div class="row">
        <div class="col-lg-3 col-md-6">
                    <div class="panel panel-primary">
                        <div class="panel-heading">
                            <div class="row">
                                <div class="col-xs-3">
                                    <i class="fa fa-users fa-5x"></i>
                                </div>
                                <div class="col-xs-9 text-right">
                                    <div class="huge"><?php echo $rowcount ?></div>
                                    <div>Total Customers</div>
                                </div>
                            </div>
                        </div>
                    </div>
        </div>


    </div>
</div></div>
<hr>

<form role="form" id="search_form" class="" action="{{ url('search-customers')}}" method="get">
<!-- <input type="hidden" name="_token" value="{{ csrf_token() }}"> -->

<div class="col-sm-3">
    <div class="input-group custom-search-form">
            <input type="text" class="form-control" id="customer_search" name="first_name" placeholder="Search Customers by name...">
            <span class="input-group-btn">
            <button class="btn btn-default" type="submit">
                    <i class="fa fa-search"></i>
            </button>
        </span>
    </div>
</div>

<div class="col-sm-3">
    <div class="input-group custom-search-form">
            <input type="text" class="form-control" id="mobile_number" name="mobile_number" placeholder="Search Customers by phone...">
            <span class="input-group-btn">
            <button class="btn btn-default" type="submit">
                    <i class="fa fa-search"></i>
            </button>
    </span>
    </div>
</div>
<br><br><br>
<div class="col-sm-3">
    <div class="input-group custom-search-form">
            <input type="text" class="form-control" id="branch" name="branch" placeholder="Search Customers by branch...">
            <span class="input-group-btn">
            <button class="btn btn-default" type="submit">
                    <i class="fa fa-search"></i>
            </button>
        </span>
    </div>
</div>

<div class="col-sm-3">
    <div class="input-group custom-search-form">
            <input type="text" class="form-control" id="customer_type" name="customer_type" placeholder="Search Customers by type...">
            <span class="input-group-btn">
            <button class="btn btn-default" type="submit">
                    <i class="fa fa-search"></i>
            </button>
    </span>
    </div>
</div>

        <input class="btn btn-primary" name="submit" type="submit" value="Search"/><br><br>
</form>

<div class="col-sm-12">
<div class="row">


    <!-- <form action="{{ url('customer/set_type')}}" class="" id="customer_set_type" method="post" role="form" /> -->

    <form action="" class="" id="customer_set_type" method="post" role="form" />

    <div style="position: absolute;top: -95px;right: 0;">
        <a class="btn btn-primary" href="{{ url('/customer/add') }}">Add new Customer</a>
        <input formaction="{{ url('customer/print_list')}}" class="btn btn-primary" name="print_data" type="submit" value="Print Data"/>
    </div><br><br><br>

    <div style="position: absolute;right: 0;margin-top: -105px;z-index: 999999;">
    <input name="_token" type="hidden" value="{{ csrf_token() }}">
    <input formaction="{{ url('customer/set_type')}}" class="btn btn-primary" name="mark_as_loyal" type="submit" value="Mark As Loyal Customers"/>
    <input formaction="{{ url('customer/set_type')}}" class="btn btn-primary" name="mark_as_regular" type="submit" value="Mark As Regular Customers"/><br><br>
    </div>

@if (Session::has('succMessage'))
   <div class="alert alert-success" style="position: absolute;margin-top: -50px;">{{ Session::get('succMessage') }}</div>
   @elseif (Session::has('errMessage'))
   <div class="alert alert-danger" style="position: absolute;margin-top: -50px;">{{ Session::get('errMessage') }}</div>
@endif

        @section ('cotable_panel_title','Customer List')
    <div class="col-sm-12">
        @section ('cotable_panel_body')

<!-- <div id="control-panel" onclick="cbclick(event)">
            <label><input type="checkbox" name="checkbox" id="checkbox-filter1"> Loyal Customers</label>&nbsp;&nbsp;
            <label><input type="checkbox" name="checkbox" id="checkbox-filter2"> Regular Customers</label>
</div> -->

        <table class="table table-hover table-striped table-bordered" id="customer_table">
            <thead>
                <tr>
                    <th><input type="checkbox" name="checkAll" id="checkAll"></th>
                    <th>Sr. No.</th>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Address</th>
                    <th>Phone</th>
                    <th>Type</th>
                    <th>Total Orders</th>
                    <th><a href="{{ $purchase_sort }}" class="ui-btn ui-icon-refresh ui-btn-icon-left">Total Amount Purchased</a></th>
                    <th>Branch</th>
                    <th>Actions</th>
                </tr>
            </thead>
                <?php
$i = 1;
?>
            <tbody>
                <tr>
            @foreach ($customers as $key=>$customer)
                    <td><input type="checkbox" name="customer_details[]" class="checkItem" value="{{$customer->id}}|{{$customer->first_name}}|{{$customer->email}}|{{$customer->mobile_number}}|{{$customer->address}}"></td>
                    <td><?php echo $key + 1;?></td>
                    <td><a href="{{ URL::to('customer/edit?id='  . $customer->id) }}">{{ $customer->first_name }}</a></td>
                    <td style ="word-break:break-all;">{{ $customer->email }}</td>
                    <td style ="word-break:break-all;"><?php if (strlen($customer->address) > 25) {
    echo $customer->address = substr($customer->address, 0, 25) . "...";
}
?>
                    </td>
                    <td>{{ $customer->mobile_number }}</td>
                    <td>{{ ($customer->customer_type != "null") ? $customer->customer_type : "-" }}</td>
                    <td>{{ $customer->total_orders }}</td>
                    <td>{{ $customer->total_purchased_amount }}</td>
                    <td>{{ $customer->branch }}</td>
                    <td class="customer-list-actions">
                    <a class="btn btn-primary edit-btn-lime-green" href="{{ URL::to('customer/edit?id='  . $customer->id) }}"><i class="fa fa-edit"></i></a>
                    @if(Auth::user()->hasRole('owner'))
                    <a class="btn btn-primary" href="{{ URL::to('customer/purchase/purchase?customer_id=    '  . $customer->id) }}"><i class="fa fa-shopping-cart"></i></a>
                    <a class="btn btn-danger delete-btn-vivid-red" onclick="return confirm('Are you sure you want to delete this record?')" href="{{ URL::to('customer/delete/delete?delete_customer=   '  . $customer->id) }}"><i class="fa fa-trash-o"></i></a>
                    @endif
                    </td>
                </tr>
        <?php $i++;?>
            @endforeach
            </tbody>
        </table>
            <div style="position: absolute;right: 50px;">
                <input class="btn btn-primary" name="send_sms" type="submit" value="Send SMS"/>
                <input class="btn btn-primary" name="send_email" type="submit" value="Send Email"/><br><br>
            </div>
        <?php
            echo $customers->render();
        ?>

            <div style="position: absolute;right: 50px;">
                <input class="btn btn-primary" name="send_sms_to_all" type="submit" value="Send SMS to all customers"/>
                <input class="btn btn-primary" name="send_email_to_all" type="submit" value="Send Email to all customers"/><br><br>
                </div><br/><br/><br/><br/>
        @endsection
        @include('widgets.panel', array('header'=>true, 'as'=>'cotable'))
        </div>
    </div>
</div>
</form>
<script type="text/javascript">
$(".send_noti").click(function(e) {
    if (!$('input:checkbox').is(':checked')) {
        alert("Please select atleast one customer to send notification!");
        e.preventDefault();
        };

$("#checkAll").click(function() {
    $('input:checkbox').not(this).prop('checked', this.checked);
});

$('form#search_form').submit(function() {

    var input1 = $.trim($('#customer_search').val());
    var input2 = $.trim($('#mobile_number').val());
    var input3 = $.trim($('#branch').val());
    var input4 = $.trim($('#customer_type').val());

    if (input1 === '' && input2 === '' && input3 === '' && input4 === '') {
        alert('Please enter your search query!');
        return false;
    }
});

</script>
@stop

我已经检查了所有可能的解决方案,但每次如果管理员点击发送短信或发送电子邮件按钮,它会显示MethodNotAllowed错误以下是显示的错误。

MethodNotAllowedHttpException in RouteCollection.php line 207:
in RouteCollection.php line 207
at RouteCollection->methodNotAllowed(array('GET', 'HEAD')) in RouteCollection.php line 194
at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD')) in RouteCollection.php line 142
at RouteCollection->match(object(Request)) in Router.php line 729
at Router->findRoute(object(Request)) in Router.php line 652
at Router->dispatchToRoute(object(Request)) in Router.php line 628
at Router->dispatch(object(Request)) in Kernel.php line 214
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 43
at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 55
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 61
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 40
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Kernel.php line 115
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
at Kernel->handle(object(Request)) in index.php line 53

3 个答案:

答案 0 :(得分:1)

如果您想点击发送电子邮件,请更改:

<form action="" class="" id="customer_set_type" method="post" role="form" />

对此:

<form action="/customer/send_email" id="customer_set_type" method="post" role="form" />

将路线改为:

Route::post('/customer/send_email', 'CustomerController@sendEmail');

如果您要发送短信和电子邮件,则需要创建两个表单或在代码中添加一些标记。

答案 1 :(得分:0)

您的表单使用POST方法设置,路由为GET

<form action="" class="" id="customer_set_type" method="post" role="form" />

Route::get('/customer/send_sms', 'CustomerController@sendNotification');
Route::get('/customer/send_email', 'CustomerController@sendEmail');

另请注意,所有按钮都像提交到同一个地方。也许最好使用JavaScript发送此请求。

答案 2 :(得分:0)

  

首先关闭:下次仅发布相关代码。不要张贴所有你的   源代码相当令人困惑。

MethodNotAllowedHttpException表示您要对GET路由进行POST,反之亦然

这些路由期待“POST”请求,如果收到GET请求,则会抛出MethodNotAllowedHttpException:

Route::get('/customer/send_sms', 'CustomerController@sendNotification');
Route::get('/customer/send_email', 'CustomerController@sendEmail');

单击下面的“提交按钮”键入=提交下面的按钮,会向服务器触发帖子请求

<input class="btn btn-primary" name="send_sms" type="submit" value="Send SMS"/>
<input class="btn btn-primary" name="send_email" type="submit" value="Send Email"/>

<强>解决方案

让您的路由接受POST请求,如下所示:

Route::post('/customer/send_sms', 'CustomerController@sendNotification');
Route::post('/customer/send_email', 'CustomerController@sendEmail');

将表单操作指向正确的路由URL

<form action="/customer/send_email" id="customer_set_type" method="post" role="form" />