Laravel 管理员编辑路由无效。 Returns404
Laravel admin edit route not working. Returns 404
尝试使用 Route::resource
和 Route::name('admin.')
后,似乎当我想通过 localhost/admin/{id}/edit
编辑用户时,它 returns 给我 404 错误。如果我在 web.php 中删除路线上的 [=52=] 它可以完美地工作,但我不想这样,因为我有两个具有编辑路线的实体。
Web.php
Route::resource('admin', 'AdminController');
/* Route::get('{username}/edit', ['as' => 'admin.edit', 'uses' => 'AdminController@edit']); */
AdminController.php
public function edit($admin) {
$user = User::where('user',$admin)-> first();
return view('admin.edit', compact('user'));
}
编辑:
edit.blade.php
@extends('layouts.app')
@section('content')
<style> .uper {margin-top: 40px;} </style>
<div class="card uper">
<div class="card-header">
Update Shows
</div>
<div class="card-body">
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div><br/>
@endif
<form method="post" action="{{ route('admin.update', $user->id) }}" enctype="multipart/form-data">
<div class="form-group">
@csrf
@method('PATCH')
<label for="name">Nom:</label>
<input type="text" class="form-control" name="name" value="{{ $user->name }}"/>
</div>
<div class="form-group">
<label for="user">Usuari:</label>
<input id="user" type="text" class="form-control @error('user') is-invalid @enderror" name="user" value="{{ $user->user }}" required autocomplete="user" autofocus>
@error('user') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror
</div>
<div class="form-group">
<label for="email">Correu:</label>
<input type="text" class="form-control" name="email" value="{{ $user->email }}"/>
@error('email') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror
</div>
<div class="form-group row">
<label for="avatar" class="col-md-4 col-form-label text-md-right">{{ __('Avatar (optional)') }}</label>
<div class="col-md-6">
<input id="avatar" type="file" class="form-control" name="avatar">
</div>
</div>
<button type="submit" class="btn btn-primary">Actualitzar</button>
</form>
</div>
</div>
@endsection
路线:列表
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
| | GET|HEAD | / | home | App\Http\Controllers\HomeController@index | web |
| | GET|HEAD | admin | admin.index | App\Http\Controllers\AdminController@index | web,roles |
| | POST | admin | admin.store | App\Http\Controllers\AdminController@store | web,roles |
| | POST | admin/crear | | App\Http\Controllers\Auth\RegisterController@register | web,roles |
| | GET|HEAD | admin/crear | register | App\Http\Controllers\Auth\RegisterController@showRegistrationForm | web,roles |
| | GET|HEAD | admin/create | admin.create | App\Http\Controllers\AdminController@create | web,roles |
| | PUT|PATCH | admin/{admin} | admin.update | App\Http\Controllers\AdminController@update | web,roles |
| | GET|HEAD | admin/{admin} | admin.show | App\Http\Controllers\AdminController@show | web,roles |
| | DELETE | admin/{admin} | admin.destroy | App\Http\Controllers\AdminController@destroy | web,roles |
| | GET|HEAD | admin/{admin}/edit | admin.edit | App\Http\Controllers\AdminController@edit | web,roles |
| | GET|HEAD | api/user | | Closure | api,auth:api |
| | PATCH | empresa/{id} | empresa.update | App\Http\Controllers\CompanyController@updateCompany | web,roles |
| | GET|HEAD | empresa/{id} | empresa.show | App\Http\Controllers\CompanyController@showCompany | web,roles |
| | GET|HEAD | empresa/{id}/edit | empresa.edit | App\Http\Controllers\CompanyController@editCompany | web,roles |
| | POST | login | | App\Http\Controllers\Auth\LoginController@login | web,guest |
| | GET|HEAD | login | login | App\Http\Controllers\Auth\LoginController@showLoginForm | web,guest |
| | POST | logout | logout | App\Http\Controllers\Auth\LoginController@logout | web |
| | POST | password/email | password.email | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail | web |
| | POST | password/reset | password.update | App\Http\Controllers\Auth\ResetPasswordController@reset | web |
| | GET|HEAD | password/reset | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web |
| | GET|HEAD | password/reset/{token} | password.reset | App\Http\Controllers\Auth\ResetPasswordController@showResetForm | web |
| | GET|HEAD | perfil | | App\Http\Controllers\HomeController@index | web |
| | POST | {user} | post.store | App\Http\Controllers\PostController@store | web,auth |
| | GET|HEAD | {user}/create | | App\Http\Controllers\PostController@create | web,auth |
| | GET|HEAD | {user}/{category}/{id} | post.show | App\Http\Controllers\PostController@show | web,auth |
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
编辑 2:
与 empresa.edit 发生同样的事情。
编辑 3:
如果我修改删除 admin 前缀的路由,它会起作用,但我想要 url:
上的 admin
Route::resource('admin', 'AdminController')->except(['edit']);
Route::get('{username}/edit', ['as' => 'admin.edit', 'uses' => 'AdminController@edit']);
我自己找的。这是因为web.php中路由的顺序。因为我在 Route::resource
之前有一个像 /{user}/create
这样的路由,所以这是导致问题的原因。无论如何,谢谢大家的帮助。
检查目录 public。如果存在管理目录重命名。
尝试使用 Route::resource
和 Route::name('admin.')
后,似乎当我想通过 localhost/admin/{id}/edit
编辑用户时,它 returns 给我 404 错误。如果我在 web.php 中删除路线上的 [=52=] 它可以完美地工作,但我不想这样,因为我有两个具有编辑路线的实体。
Web.php
Route::resource('admin', 'AdminController');
/* Route::get('{username}/edit', ['as' => 'admin.edit', 'uses' => 'AdminController@edit']); */
AdminController.php
public function edit($admin) {
$user = User::where('user',$admin)-> first();
return view('admin.edit', compact('user'));
}
编辑:
edit.blade.php
@extends('layouts.app')
@section('content')
<style> .uper {margin-top: 40px;} </style>
<div class="card uper">
<div class="card-header">
Update Shows
</div>
<div class="card-body">
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div><br/>
@endif
<form method="post" action="{{ route('admin.update', $user->id) }}" enctype="multipart/form-data">
<div class="form-group">
@csrf
@method('PATCH')
<label for="name">Nom:</label>
<input type="text" class="form-control" name="name" value="{{ $user->name }}"/>
</div>
<div class="form-group">
<label for="user">Usuari:</label>
<input id="user" type="text" class="form-control @error('user') is-invalid @enderror" name="user" value="{{ $user->user }}" required autocomplete="user" autofocus>
@error('user') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror
</div>
<div class="form-group">
<label for="email">Correu:</label>
<input type="text" class="form-control" name="email" value="{{ $user->email }}"/>
@error('email') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror
</div>
<div class="form-group row">
<label for="avatar" class="col-md-4 col-form-label text-md-right">{{ __('Avatar (optional)') }}</label>
<div class="col-md-6">
<input id="avatar" type="file" class="form-control" name="avatar">
</div>
</div>
<button type="submit" class="btn btn-primary">Actualitzar</button>
</form>
</div>
</div>
@endsection
路线:列表
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
| | GET|HEAD | / | home | App\Http\Controllers\HomeController@index | web |
| | GET|HEAD | admin | admin.index | App\Http\Controllers\AdminController@index | web,roles |
| | POST | admin | admin.store | App\Http\Controllers\AdminController@store | web,roles |
| | POST | admin/crear | | App\Http\Controllers\Auth\RegisterController@register | web,roles |
| | GET|HEAD | admin/crear | register | App\Http\Controllers\Auth\RegisterController@showRegistrationForm | web,roles |
| | GET|HEAD | admin/create | admin.create | App\Http\Controllers\AdminController@create | web,roles |
| | PUT|PATCH | admin/{admin} | admin.update | App\Http\Controllers\AdminController@update | web,roles |
| | GET|HEAD | admin/{admin} | admin.show | App\Http\Controllers\AdminController@show | web,roles |
| | DELETE | admin/{admin} | admin.destroy | App\Http\Controllers\AdminController@destroy | web,roles |
| | GET|HEAD | admin/{admin}/edit | admin.edit | App\Http\Controllers\AdminController@edit | web,roles |
| | GET|HEAD | api/user | | Closure | api,auth:api |
| | PATCH | empresa/{id} | empresa.update | App\Http\Controllers\CompanyController@updateCompany | web,roles |
| | GET|HEAD | empresa/{id} | empresa.show | App\Http\Controllers\CompanyController@showCompany | web,roles |
| | GET|HEAD | empresa/{id}/edit | empresa.edit | App\Http\Controllers\CompanyController@editCompany | web,roles |
| | POST | login | | App\Http\Controllers\Auth\LoginController@login | web,guest |
| | GET|HEAD | login | login | App\Http\Controllers\Auth\LoginController@showLoginForm | web,guest |
| | POST | logout | logout | App\Http\Controllers\Auth\LoginController@logout | web |
| | POST | password/email | password.email | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail | web |
| | POST | password/reset | password.update | App\Http\Controllers\Auth\ResetPasswordController@reset | web |
| | GET|HEAD | password/reset | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web |
| | GET|HEAD | password/reset/{token} | password.reset | App\Http\Controllers\Auth\ResetPasswordController@showResetForm | web |
| | GET|HEAD | perfil | | App\Http\Controllers\HomeController@index | web |
| | POST | {user} | post.store | App\Http\Controllers\PostController@store | web,auth |
| | GET|HEAD | {user}/create | | App\Http\Controllers\PostController@create | web,auth |
| | GET|HEAD | {user}/{category}/{id} | post.show | App\Http\Controllers\PostController@show | web,auth |
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
编辑 2:
与 empresa.edit 发生同样的事情。
编辑 3:
如果我修改删除 admin 前缀的路由,它会起作用,但我想要 url:
上的 adminRoute::resource('admin', 'AdminController')->except(['edit']);
Route::get('{username}/edit', ['as' => 'admin.edit', 'uses' => 'AdminController@edit']);
我自己找的。这是因为web.php中路由的顺序。因为我在 Route::resource
之前有一个像 /{user}/create
这样的路由,所以这是导致问题的原因。无论如何,谢谢大家的帮助。
检查目录 public。如果存在管理目录重命名。