Laravel管理员编辑路径不起作用。返回404

时间:2019-12-10 09:58:24

标签: php laravel controller routes admin

尝试使用Route::resourceRoute::name('admin.')之后,似乎当我想通过localhost/admin/{id}/edit编辑用户时,它返回404错误。如果我在web.php中删除路由上的“ admin”,则可以正常运行,但我不希望这样,因为我有两个具有编辑路由的实体。

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:

如果我修改删除管理员前缀的路由,则可以使用,但是我希望管理员使用网址:

Route::resource('admin', 'AdminController')->except(['edit']);
Route::get('{username}/edit', ['as' => 'admin.edit', 'uses' => 'AdminController@edit']);

2 个答案:

答案 0 :(得分:0)

我自己找到的。这是因为 web.php 中路由的顺序。由于我在/{user}/create之前有一条类似Route::resource的路线,因此引起了问题。无论如何,谢谢大家的帮助。

答案 1 :(得分:0)

检查目录public。如果存在,则将admin dir重命名。