此操作未经授权 403 / 无法在 Laravel 7 上加载表单页面

问题描述 投票:0回答:1

我无法打开编辑个人资料页面(表单),我不确定出了什么问题,因为我一开始可以访问它。 然后我到处写代码(没有触及任何与编辑配置文件相关的文件/类),但突然我无法访问它。

错误代码:403 此操作未经授权

Laravel 7


路线

Route::get('/profile/{user}/edit','ProfileController@edit')
        ->name('profile.edit')
        ->middleware('auth');
Route::patch('/profile/{user}','ProfileController@update')
        ->name('profile.update')
        ->middleware('auth');

index.blade.php

<li class="nav-item dropdown">
    <a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
        {{ Auth::user()->name }} <span class="caret"></span>
    </a>

    <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
        <a href="{{ route('profile.edit',['user'=>Auth::user()->id ]) }}" class="dropdown-item">Edit Profile</a>

        @if(Auth::user()->role == 'Customer')
        <a href="{{ route('order.show',['user'=>Auth::user()->id]) }}" class="dropdown-item">Purchase History</a>
        @endif

        <a class="dropdown-item" href="{{ route('logout') }}"
           onclick="event.preventDefault();
                         document.getElementById('logout-form').submit();">
            {{ __('Logout') }}
        </a>

        <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
            @csrf
        </form>
    </div>
</li>

ProfileController.php

class ProfileController extends Controller
{
    public function edit(User $user){
        $this->authorize('update',$user->profile);
        return view('profiles.edit', compact('user'));
    }

    public function update($user){
        $data = request()->validate([
            'phonenumber'=> ['required','numeric'] ,
            'country'=>['required','string','max:255'],
            'city'=>['required','string','max:255'],
            'address'=>['required','string','max:255'],
            'zipcode'=>['required','digits:5'],

        ]);

        $name = request()->validate([
            'name'=>['required','string','max:255'],
        ]);

        auth()->user()->update($name);
        auth()->user()->profile->update($data);

        return redirect("/");
    }
}

edit.blade.php

@section('content')
<div class="container sixtyvh">
    <form method="POST" action="{{ route('profile.update',['user'])  }}" enctype="multipart/form-data">
        @csrf
        @method('PATCH')

        <div class="row">

            <div class="col-8 mx-auto">
                <h3 class="offset-md-5">Edit Profile</h3>
                <hr>
                <div class="form-group row">
                    <label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name:') }}</label>

                    <div class="col-md-6">
                        <input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="{{ old('name') ?? $user->name }}" required autocomplete="name" autofocus>

                        @error('name')
                            <span class="invalid-feedback" role="alert">
                                <strong>{{ $message }}</strong>
                            </span>
                        @enderror
                    </div>
                </div>

                <div class="form-group row">
                    <label for="phonenumber" class="col-md-4 col-form-label text-md-right">{{ __('Phone Number:') }}</label>

                    <div class="col-md-6">
                        <input id="phonenumber" type="text" class="form-control @error('phonenumber') is-invalid @enderror" name="phonenumber" value="{{ old('phonenumber') ?? $user->profile->phonenumber  }}" required autocomplete="phonenumber" autofocus>

                        @error('phonenumber')
                            <span class="invalid-feedback" role="alert">
                                <strong>{{ $message }}</strong>
                            </span>
                        @enderror
                    </div>
                </div>

                <div class="form-group row">
                    <label for="country" class="col-md-4 col-form-label text-md-right">{{ __('Country:') }}</label>

                    <div class="col-md-6">
                        <input id="country" type="text" class="form-control @error('country') is-invalid @enderror" name="country" value="{{ old('country') ?? $user->profile->country  }}" required autocomplete="country" autofocus>

                        @error('country')
                            <span class="invalid-feedback" role="alert">
                                <strong>{{ $message }}</strong>
                            </span>
                        @enderror
                    </div>
                </div>

                <div class="form-group row">
                    <label for="city" class="col-md-4 col-form-label text-md-right">{{ __('City:') }}</label>

                    <div class="col-md-6">
                        <input id="city" type="text" class="form-control @error('city') is-invalid @enderror" name="city" value="{{ old('city') ?? $user->profile->city }}" required autocomplete="city" autofocus>

                        @error('city')
                            <span class="invalid-feedback" role="alert">
                                <strong>{{ $message }}</strong>
                            </span>
                        @enderror
                    </div>
                </div>

                <div class="form-group row">
                    <label for="address" class="col-md-4 col-form-label text-md-right">{{ __('Address:') }}</label>

                    <div class="col-md-6">
                        <input id="address" type="text" class="form-control @error('address') is-invalid @enderror" name="address" value="{{ old('address') ?? $user->profile->address }}" required autocomplete="address" autofocus>

                        @error('address')
                            <span class="invalid-feedback" role="alert">
                                <strong>{{ $message }}</strong>
                            </span>
                        @enderror
                    </div>
                </div>

                <div class="form-group row">
                    <label for="zipcode" class="col-md-4 col-form-label text-md-right">{{ __('Zipcode:') }}</label>

                    <div class="col-md-6">
                        <input id="zipcode" type="text" class="form-control @error('zipcode') is-invalid @enderror" name="zipcode" value="{{ old('zipcode') ?? $user->profile->zipcode }}" required autocomplete="zipcode" autofocus>

                        @error('zipcode')
                            <span class="invalid-feedback" role="alert">
                                <strong>{{ $message }}</strong>
                            </span>
                        @enderror
                    </div>
                </div>

                <div class="form-group row mb-0">
                    <div class="col-md-6 offset-md-4">
                        <button type="submit" class="button-primary w-100">
                            {{ __('Done') }}
                        </button>
                    </div>
                </div>
            </div>
        </div>


    </form>
</div>
@endsection

发布政策

class ProfilePolicy
{
    use HandlesAuthorization;

    public function update(User $user, Profile $profile)
    {
        return $user->id === $profile->user_id;
    }
}


AuthServiceProvider

class AuthServiceProvider extends ServiceProvider
{
    protected $policies = [
        // 'App\Model' => 'App\Policies\ModelPolicy',
        Post::class => PostPolicy::class,
    ];

    public function boot()
    {
        $this->registerPolicies();

        //
    }
}

我是 Laravel 新手,提前感谢您对我的帮助!

php laravel forms unauthorized
1个回答
0
投票

将配置文件策略添加到

$policies
中的
AuthServiceProvider
。 像这样:

protected $policies = [
    // 'App\Model' => 'App\Policies\ModelPolicy',
    Post::class => PostPolicy::class,
    Profile::class => ProfilePolicy::class,
]
© www.soinside.com 2019 - 2024. All rights reserved.