2017-03-17 165 views
1

我routes.php文件文件爲客戶頁面,我面臨的問題,當我點擊發送電子郵件和發短信按鈕Laravel投擲MethodNotAllowedHttpException錯誤

Route::get('/customer', '[email protected]'); 
Route::get('/customer/add', '[email protected]'); 
Route::post('/customer/add', '[email protected]'); 
Route::get('/customer/delete/{id}', '[email protected]'); 
Route::get('/customer/edit', '[email protected]'); 
Route::post('/customer/edit', '[email protected]'); 
Route::get('/search-customers', '[email protected]'); 
Route::post('/customer/set_type', '[email protected]'); 
Route::get('/customer/send_sms', '[email protected]'); 
Route::get('/customer/send_email', '[email protected]'); 

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 
+0

下一次不粘貼所有代碼但只是相關的代碼,因爲現在很難找到合適的部分。 –

+0

它可能是一些路線錯誤,你試圖張貼在獲取路線或? –

+0

某些東西與您的行爲和路線不匹配。如果你的表單動作是get和route將是Route :: get,如果你的表單動作是post,那麼你的路由將是Route :: post – Muthu17

回答

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', '[email protected]'); 

如果您想發送短信和電子郵件,你需要創建吃了兩種形式或在代碼中添加一些標誌。而路線是爲GET

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

Route::get('/customer/send_sms', '[email protected]'); 
Route::get('/customer/send_email', '[email protected]'); 

另外請注意,所有按鈕都喜歡提交到同一個地方

+1

非常感謝,指出這對我來說就像魅力。 –

0

您的形式設置與POST方法。也許最好用JavaScript來發送這些請求。

+0

感謝您的回覆它絕對幫助我。但是,上述評論更好地集中在我認爲這是正確的。非常感謝 –

0

首先關閉:下次發表只有相關的代碼。不要發佈你所有的源代碼,它相當混亂。

MethodNotAllowedHttpException意味着你要麼做一個POST到GET路由或反之亦然

這些路由期待「POST」請求,將拋出MethodNotAllowedHttpException如果他們收到一個GET請求,而不是:

Route::get('/customer/send_sms', '[email protected]'); 
Route::get('/customer/send_email', '[email protected]'); 

通過點擊這些按鈕下面這些「提交按鈕」 類型=提交後請求被激發到服務器

<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', '[email protected]'); 
Route::post('/customer/send_email', '[email protected]'); 

點形式作用到正確的路線URL

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

非常感謝下次我會記住這一點 –