【问题标题】:Forbidden when trying to patch User尝试修补用户时被禁止
【发布时间】:2015-06-13 01:38:22
【问题描述】:

我正在尝试通过修补更新来更新用户的个人资料。 补丁似乎正在通过,但是当按下“保存个人资料”时,我只是转到一个空白页面,上面写着:“禁止”。

这是我的代码:

ProfileController.php

<?php namespace App\Http\Controllers;

use Auth;

use App\User;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\UpdateUserRequest;

use Illuminate\Http\Request;

class ProfileController extends Controller {

    public function __construct()
    {
        $this->middleware('auth');
    }

    public function show()
    {
        return view('pages.profile.profile');
    }

    public function search($username)
    {
        $user = User::whereUsername($username)->first();

        return view('pages.profile.showprofile', compact('user'));
    }

    public function edit() 
    {
        $user = Auth::user();

        return view('pages.profile.editprofile')->withUser($user);
    }

    public function update(UpdateUserRequest $request, User $user) 
    {
        return 'update user';
    }


}

Routes.php

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

# Home
Route::get('/', 'StaticPagesController@home');

# Profile
#User binding
Route::bind('user', function($username) {
$user = App\User::find($username)->first();
});
Route::get('profile', 'ProfileController@show');
Route::get('profile/edit', 'ProfileController@edit');
Route::get('profile/{username}', 'ProfileController@search');
Route::patch('user/{username}', 'ProfileController@update');

# Calendar
Route::get('calendar-php', 'CalendarController@index');
Route::get('calendar', 'CalendarController@show');

# Authentication
Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);

editprofile.blade.php

@extends('masterpage')
...
{!! Form::model($user, ['url' => 'user/' . $user->username, 'method' => 'PATCH']) !!}
    <div class="form-group form-horizontal">
        <div class="form-group">
                {!! Form::label('username', 'Username:', ['class' => 'col-md-4 control-label']) !!}
            <div class="col-md-6">
                <label class="align-left">{{ $user->username}}<label>       
            </div>  
        </div>

        <div class="form-group">
                {!! Form::label('email', 'E-mail:', ['class' => 'col-md-4 control-label']) !!}
            <div class="col-md-6">
                <label class="align-left">{{ $user->email}}<label>  
            </div>  
        </div>

        <div class="form-group">
                {!! Form::label('name', 'Name:', ['class' => 'col-md-4 control-label']) !!}
            <div class="col-md-6">
                <label class="align-left">{{ $user->name}} {{ $user->lastname}}<p>  
            </div>  
        </div>

        <br />

        <div class="form-group">
                {!! Form::label('city', 'City:', ['class' => 'col-md-4 control-label']) !!}
            <div class="col-md-6">
                {!! Form::Text('city', null, ['class' => 'form-control']) !!}
            </div>  
        </div>

        <div class="form-group">
                {!! Form::label('country', 'Country:', ['class' => 'col-md-4 control-label']) !!}
            <div class="col-md-6">
                {!! Form::Text('country', null, ['class' => 'form-control']) !!}
            </div>  
        </div>

        <div class="form-group">
                {!! Form::label('phone', 'Phone:', ['class' => 'col-md-4 control-label']) !!}
            <div class="col-md-6">
                {!! Form::Text('phone', null, ['class' => 'form-control']) !!}
            </div>  
        </div>

        <div class="form-group">
                {!! Form::label('twitter', 'Twitter link:', ['class' => 'col-md-4 control-label']) !!}
            <div class="col-md-6">
                {!! Form::Text('twitter', null, ['class' => 'form-control']) !!}
            </div>  
        </div>

        <div class="form-group">
                {!! Form::label('facebook', 'Facebook link:', ['class' => 'col-md-4 control-label']) !!}
            <div class="col-md-6">
                {!! Form::Text('facebook', null, ['class' => 'form-control']) !!}
            </div>  
        </div>

        <div class="form-group">
            <div class="col-md-6 col-md-offset-4">
                {!! Form::submit('Save Profile', ['class' =>  'btn btn-primary']) !!}
            </div>
        </div> 

        </div>  
    </div>
{!! Form::close() !!}
...

这是我按下保存配置文件按钮后得到的页面:

我已经搜索过这个错误,这就是我发现的: // 禁止 App::abort(403, '访问被拒绝'); 虽然我正在尝试更新我自己的个人资料。 有人知道它为什么这样做吗?

【问题讨论】:

  • 我会说是的,但我现在已经在这个 Laravel 项目上投入了很多。对我来说,从头开始重新启动这个项目对我来说是不明智的。不过,我会在未来的项目中看看它。谢谢你的提议。
  • 另外,你可能对 Laravel 的新 Lumen 框架感兴趣。它具有相当的性能和易用性。

标签: php eloquent laravel-5 patch blade


【解决方案1】:

我知道您可能已经弄清楚了,但为了使这篇文章更有帮助,我想发布此错误的可能原因。

您似乎正在使用自定义请求类“UpdateUserRequest”。如果您使用“php artisan make:request”执行此操作,您会注意到请求文件中的“authorize()”方法默认返回“false”。您可以将其更改为返回“true”,或者在此方法中编写您自己的授权逻辑。

【讨论】:

  • 这对我有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-12
  • 2015-08-20
  • 1970-01-01
  • 2013-08-06
  • 2015-09-18
  • 1970-01-01
相关资源
最近更新 更多