我目前正在Laravel4上進行演示應用。演示應用程序在數據庫中有一些用戶。我想一一編輯它們。我有一個方法「postUpdate」,然而,編輯(http://localhost/testlaravell/users/5/edit)從列表中的用戶時,我看到發生錯誤 -Laravel 4 - 路由[users.postUpdate]未定義
ErrorException (E_ERROR)
Route [users.postUpdate] not defined. (View: D:\wamp\www\testlaravell\local\app\views\users\edit.blade.php).
我有代碼在routes.php文件:
Route::get('/', function()
{
return View::make('hello');
});
Route::get('users/{all}/edit', '[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
我想念的東西我不知道。有人能幫我嗎?
您的路線不具名的路線。你必須在路由器中以爆炸性的方式命名它:Route :: post('users/update','UserController @ postUpdate') - > name('users.postUpdate'); – naneri
建議:您可能想使用[REST全局資源](https://laravel.com/docs/4.2/controllers#restful-resource-controllers)控制器,而不是使用隱式控制器混合基本路由。 – Mirceac21
@naneri在路由中實現代碼後,出現錯誤 - 調用未定義的方法Illuminate \ Routing \ Route :: name() –