我尝试通过修补更新来更新用户的个人资料。 补丁似乎正在进行,但是当按下“保存个人资料”时我只是转到一个空白页面说明:'禁止'。
这是我的代码:
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,&#39; Access denied&#39;); 虽然我试图更新自己的个人资料。 任何人都知道为什么要这样做?
答案 0 :(得分:1)
我意识到你可能已经弄明白了,但是为了使这篇文章更有帮助,我想发布一个可能的错误原因。
看起来您正在使用自定义请求类“UpdateUserRequest”。如果你使用“php artisan make:request”执行此操作,你会注意到你的请求文件中的“authorize()”方法默认会返回“false”。您可以将其更改为返回“true”,或者在此方法中编写自己的授权逻辑。