我想从列表中修改用户。我在routes.php中有代码:
Route::get('users/{all}/edit', 'UserController@getEdit');
Route::post('users/update', ['as' => 'users.postUpdate', 'uses' => 'UserController@postUpdate']);
Route::controller('users', 'UserController');
在UserController.php中,我编写了以下脚本进行编辑和更新:
public function getEdit($id)
{
//
$user = User::find($id);
if (is_null($user))
{
return Redirect::to('users/all');
}
return View::make('users.edit', compact('user'));
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function postUpdate($id)
{
//
$input = Input::all();
$validation = Validator::make($input, User::$rules);
if ($validation->passes())
{
//$user = User::find($id);
$user = User::find($id);
$user->username = Input::get('username');
$user->name = Input::get('name');
$user->email = Input::get('email');
$user->phone = Input::get('phone');
$user->password = Hash::make(Input::get('password'));
$user->save();
return Redirect::route('users.getIndex', $id);
}
return Redirect::route('users.getEdit', $id)
->withInput()
->withErrors($validation)
->with('message', 'There were validation errors.');
}
edit.blade.php下的代码如下:
@extends('users.user')
@section('main')
<h1>Edit User</h1>
{{ Form::model($user, array('method' => 'PATCH', 'route' => array('users.postUpdate', $user->id))) }}
<ul>
<li>
{{ Form::label('username', 'Username:') }}
{{ Form::text('username') }}
</li>
<li>
{{ Form::label('password', 'Password:') }}
{{ Form::text('password') }}
</li>
<li>
{{ Form::label('email', 'Email:') }}
{{ Form::text('email') }}
</li>
<li>
{{ Form::label('phone', 'Phone:') }}
{{ Form::text('phone') }}
</li>
<li>
{{ Form::label('name', 'Name:') }}
{{ Form::text('name') }}
</li>
<li>
{{ Form::submit('Update', array('class' => 'btn btn-info')) }}
{{ link_to_route('users.getAll', 'Cancel', $user->id, array('class' => 'btn')) }}
</li>
</ul>
{{ Form::close() }}
@if ($errors->any())
<ul>
{{ implode('', $errors->all('<li class="error">:message</li>')) }}
</ul>
@endif
@stop
编辑屏幕打开很好。但是,在修改值并提交表单时,URL显示为http://localhost/testlaravell/users/update?5,并且出现错误 -
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
Controller method not found.
请帮助我如何解决这个问题。
答案 0 :(得分:2)
此处的问题是您使用PATCH
方法但在控制器中您使用方法postUpdate
(对于routes.php
也是如此)。
你应该做的是:
postUpdate
更改为putUpdate
更改
Route::post('users/update', ['as' => 'users.postUpdate', 'uses' => 'UserController@postUpdate']);
到
Route::put('users/update/{id}', ['as' => 'users.putUpdate', 'uses' => 'UserController@putUpdate']);
更改表单
{{ Form::model($user, array('method' => 'PATCH', 'route' => array('users.postUpdate', $user->id))) }
到
{{ Form::model($user, array('method' => 'PUT', 'route' => array('users.putUpdate', $user->id))) }
答案 1 :(得分:0)
像这样定义你的路线:
Route::resource('users', 'UsersController');
改变它:
{{ Form::model($user, array('method' => 'PATCH', 'route' => array('users.postUpdate', $user->id))) }}
到
{!! Form::model($user, array('method' => 'PATCH', 'route' => array('users.update', $user->id))) !!}
按照以下方式编写控制器:
public function edit($id)
{
$user = User::find($id);
if (is_null($user))
{
return Redirect::to('users/all');
}
return View::make('users.edit', compact('user'));
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
$input = Input::all();
$validation = Validator::make($input, User::$rules);
if ($validation->passes())
{
$user = User::find($id);
$user->username = Input::get('username');
$user->name = Input::get('name');
$user->email = Input::get('email');
$user->phone = Input::get('phone');
$user->password = Hash::make(Input::get('password'));
$user->save();
return Redirect::route('users.getIndex', $id);
}
return Redirect::route('users.getEdit', $id)
->withInput()
->withErrors($validation)
->with('message', 'There were validation errors.');
}
希望,它会起作用。