2017-07-16 132 views
1

我與LaravelMethodNotAllowedHttpException Laravel 5.4

現在開始我得到一個MethodNotAllowedHttpException嘗試從一個認證的一個添加新用戶。

這是輸入形式(add.blade.php)

<form method="POST" action="{{ action('Backend\[email protected]') }}"> 

    {{csrf_field()}} 


    <div class="form-group"> 
    <label for="first_name">First Name:</label> 
    <input id="first_name" type="text" class="form-control" name="first_name" value="{{ old('first_name') }}" placeholder="Your Name" required> 
    <!--<input type="text" class="form-control" id="name" name="name"> --> 
    </div> 

    <div class="form-group"> 
    <label for="last_name">Last Name:</label> 
    <input id="last_name" type="text" class="form-control" name="last_name" value="{{ old('first_name') }}" placeholder="Your Surname" required> 
    <!--<input type="text" class="form-control" id="name" name="name"> --> 
    </div> 

    <div class="form-group"> 
    <label for="email">E-Mail:</label> 
    <input id="email" type="text" class="form-control" name="email" value="{{ old('email') }}" placeholder="Your Email" required> 
    <!--<input type="email" class="form-control" id="email" name="email">--> 
    </div> 

    <div class="form-group"> 
    <label for="password">Password:</label> 
    <input id="password" type="password" class="form-control" name="password" placeholder="Your Password" required> 
    <!--<input type="password" class="form-control" id="password" name="password">--> 
    </div> 


    <div class="form-group"> 
    <button type="submit" class="btn btn-primary">Register</button> 
    </div> 

    @include ('layouts.errors') 

</form> 

這是用戶控制器(UserController.php),請檢查存儲方法

<?php 

namespace App\Http\Controllers\Backend; 

use App\Http\Controllers\Controller; 
use App\User; 
use Illuminate\Http\Request; 
use Illuminate\Support\Str; 

class UserController extends Controller { 
    public function __construct() { 
     $this->middleware('auth'); 

    } 

    public function getIndex() { 
     $users = User::orderBy('created_at', 'DESC')->paginate(10); 
     return view('backend.user.list', compact('users')); 
    } 

    public function getAdd() { 
     // schermata di aggiunta del nuovo autore 
     return view('backend.user.add'); 
    } 

    public function store(Request $request) { 
     // elaborazione dei dati ed effettiva aggiunta dell'autore 

     $this->validate(request(), [ 

      'first_name' => 'required', 
      'last_name' => 'required', 
      'email' => 'required|email|unique:users', 
      'password' => 'required|confirmed', 

     ]); 

     //Create and Save the Users 
     $first_name = $request->first_name; 
     $last_name = $request->last_name; 
     $email = $request->email; 
     $password = $request->password; 
     $slug = Str::slug($first_name . $last_name); 

     $user = User::create([ 
      'first_name' => $first_name, 
      'last_name' => $last_name, 
      'email' => $email, 
      'slug' => $slug, 
      'password' => bcrypt($password), 

     ]); 

     return redirect('backend/indexuser'); 

    } 

    public function getDelete($userId, Request $request) { 
     $userToDelete = User::find($userId); 
     $userToDelete->delete(); 
     //$userId->delete(); 

     $request->session()->flash('success_message', 'User Deleted'); 
     return redirect('backend/indexuser'); 
    } 
} 

這是路由(web.php)

Route::get('backend/indexuser', 'Backend\[email protected]'); 
Route::get('backend/adduser', 'Backend\[email protected]'); 
Route::post('backend/adduser', 'Backend\[email protected]'); 
Route::get('backend/indexuser/delete/{id}', 'Backend\[email protected]'); 

用戶登記即時重定向到http://192.168.33.10/myBlog/public/logout與MethodN後otAllowedHttpException屏幕

這是PHP工匠路線:列表

+--------+----------+-------------------------------+------+------------------------------------------------------------+--------------+ 
| Domain | Method | URI       | Name | Action              | Middleware | 
+--------+----------+-------------------------------+------+------------------------------------------------------------+--------------+ 
|  | GET|HEAD |/       |  | App\Http\Controllers\[email protected]   | web   | 
|  | GET|HEAD | api/user      |  | Closure             | api,auth:api | 
|  | GET|HEAD | articolo/{slug}    |  | App\Http\Controllers\[email protected]  | web   | 
|  | GET|HEAD | autore/{slug}     |  | App\Http\Controllers\[email protected]   | web   | 
|  | GET|HEAD | backend/adduser    |  | App\Http\Controllers\Backend\[email protected]   | web,auth  | 
|  | POST  | backend/adduser    |  | App\Http\Controllers\Backend\[email protected]   | web,auth  | 
|  | GET|HEAD | backend/indexuser    |  | App\Http\Controllers\Backend\[email protected]  | web,auth  | 
|  | GET|HEAD | backend/indexuser/delete/{id} |  | App\Http\Controllers\Backend\[email protected]  | web,auth  | 
|  | GET|HEAD | categoria/{slug}    |  | App\Http\Controllers\[email protected]  | web   | 
|  | GET|HEAD | dashboard      |  | App\Http\Controllers\Backend\[email protected] | web,auth  | 
|  | GET|HEAD | login       | home | App\Http\Controllers\Backend\[email protected]  | web,guest | 
|  | POST  | login       |  | App\Http\Controllers\Backend\[email protected]  | web,guest | 
|  | GET|HEAD | logout      |  | App\Http\Controllers\Backend\[email protected] | web,auth  | 
|  | GET|HEAD | register      |  | App\Http\Controllers\Backend\[email protected] | web   | 
|  | POST  | register      |  | App\Http\Controllers\Backend\[email protected] | web 

有什麼不對? 謝謝

\\\\\\\\\\\\\\\\\ UPDATE 1 //////////////////////////

檢查了我提交的HTML。

<form method="POST" action="http://192.168.33.10/myBlog/public/backend/adduser"> 

      <input type="hidden" name="_token" value="cWbRF1A7UWEzPWqtmVhVocbzQdkwKgc8dABne28v"> 


      <div class="form-group"> 
      <label for="first_name">First Name:</label> 
      <input id="first_name" type="text" class="form-control" name="first_name" value="" placeholder="Your Name" required> 
      <!--<input type="text" class="form-control" id="name" name="name"> --> 
      </div> 

這是我在中間件夾RedirectIfAuthenticated.php

<?php 

namespace App\Http\Middleware; 

use Closure; 
use Illuminate\Support\Facades\Auth; 

class RedirectIfAuthenticated { 
    /** 
    * Handle an incoming request. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param \Closure $next 
    * @param string|null $guard 
    * @return mixed 
    */ 
    public function handle($request, Closure $next, $guard = null) { 
    if (Auth::guard($guard)->check()) { 

     return redirect('dashboard'); 

    } 

    return $next($request); 
    } 
} 

這是Handler.php

 protected function unauthenticated($request, AuthenticationException $exception) { 
      if ($request->expectsJson()) { 
       return response()->json(['error' => 'Unauthenticated.'], 401); 
      } 

      //return redirect()->guest(route('login')); 
      //redirect se l'utente prova ad accedere da guest, reindirizzo a login 
      return redirect()->home(); 
     } 
    } 

未經驗證的方法在/storage/logs/laravel.log我可以最後沒有發現任何有趣的東西, 這些是最後一行

#47 /var/www/public/myBlog/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure)) 
#48 /var/www/public/myBlog/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) 
#49 /var/www/public/myBlog/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request)) 
#50 /var/www/public/myBlog/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\Pipeline\Pipeline->then(Object(Closure)) 
#51 /var/www/public/myBlog/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request)) 
#52 /var/www/public/myBlog/public/index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request)) 
#53 {main} 

\\\\\\\\更新2 //////////////////////////

Log From storage/logs/laravel.log

+0

數據錄入在表中完成? –

+0

你確定要發佈到'/ backend/adduser'嗎?我想你'行動(「後端\ UserController的@ store'段沒有返回預期的URI – piotr

+0

有你在用戶模型中添加廢料場$可填寫陣列 –

回答

0

交換從獲取到後註銷RUOTE,然後我搬到從導航欄(主佈局頁)表單動作的儀表盤頁面,

<p>Benvenuto {{ Auth::user()->first_name }} ! Scegli cosa vuoi fare usando il menu qui in alto.</p> 

    <form action="{{ action('Backend\[email protected]') }}" method="POST" accept-charset="utf-8"> 
     <input type="hidden" name="_token" value="{{ csrf_token() }}"> 
      <div class="form-group"> 
       <button type="submit" class="btn btn-danger">Log Out</button> 
      </div> 
    </form> 

@endsection 

出了錯現在的問題得到解決在主佈局刀片中。當我嘗試使用'Backend \ UserController @ store'操作創建新用戶時,註銷操作也被調用。 我無法弄清楚,但現在我解決了它,用儀表板頁面中的按鈕替換「navbar註銷」。

Regards

0

爲什麼不只是使用url()函數?

<form method="POST" action="{{ url('backend/adduser') }}"> 
0

第一: 檢查您呈現的HTML。我認爲您的表單中有action部分內容會讓人感到奇怪。

如果問題仍然存在 檢查您是否已經觸及auth中間件

如果問題仍然存在 檢查日誌(/storage/logs/laravel.log),並與我們分享。

+0

你好@piotr在此先感謝。請檢查主帖子中的更新1。 – romeoenrico

相關問題