我想從列表中修改用戶。我有代碼的routes.php文件:Laravel 4 - 未找到控制器方法
Route::get('users/{all}/edit', '[email protected]');
Route::post('users/update', ['as' => 'users.postUpdate', 'uses' => '[email protected]']);
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
編輯屏幕開放良好。然而,當修改值並提交表單,網址顯示爲http://localhost/testlaravell/users/update?5並出現錯誤 -
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
Controller method not found.
請幫助我,我怎麼能解決這個問題。
另一個正確答案@Marcin。非常感謝。你是一個Laravel碩士。 –