我與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
數據錄入在表中完成? –
你確定要發佈到'/ backend/adduser'嗎?我想你'行動(「後端\ UserController的@ store'段沒有返回預期的URI – piotr
有你在用戶模型中添加廢料場$可填寫陣列 –