所以基本上我在controller.PHP中有一个存储功能和更新功能,并且想知道如何在需要时指定使用哪种方法.我的代码进度如下所示.有人有解决方案吗?
Route::resource('manage_accounts', 'ManageAccountsController',
['only' => ['index', 'store', 'update']]);
view.blade.PHP
<button class="btn btn-sm btn-warning" type="button"
data-toggle="modal" data-target="#register" value="{{ $user->id }}">Edit <i class="glyphicon glyphicon-edit"></i></button>
<button type="button" class="btn btn-info btn-md" data-toggle="modal" data-target="#register">Register New User</button>
<!-- Modal -->
<div id="register" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">User information</h4>
</div>
<div class="modal-body">
<form class="form-horizontal" role="form" method="POST" action="/manage_accounts" novalidate>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="control-label col-sm-3" for="name">Username:</label>
<div class="col-sm-5 @if ($errors->has('name')) has-error @endif">
<input type="text" class="form-control" type="hidden" id="name" name="name" placeholder="Enter username">
@if ($errors->has('name')) <p class="help-block">{{ $errors->first('name') }}</p> @endif
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="password">Password:</label>
<div class="col-sm-5 @if ($errors->has('password')) has-error @endif">
<input type="password" class="form-control" type="hidden" id="password" name="password" placeholder="Enter login password">
@if ($errors->has('password')) <p class="help-block">{{ $errors->first('password') }}</p> @endif
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="password_confirm">Confirm Password:</label>
<div class="col-sm-5 @if ($errors->has('password_confirm')) has-error @endif">
<input type="password" class="form-control" type="hidden" id="password_confirm" name="password_confirm" placeholder="Re-type password again">
@if ($errors->has('password_confirm')) <p class="help-block">{{ $errors->first('password_confirm') }}</p> @endif
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="email">Email:</label>
<div class="col-sm-5 @if ($errors->has('email')) has-error @endif">
<input type="email" class="form-control" type="hidden" id="email" name="email" placeholder="Enter email address">
@if ($errors->has('email')) <p class="help-block">{{ $errors->first('email') }}</p> @endif
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="mobile">Phone Number:</label>
<div class="col-sm-5 @if ($errors->has('mobile')) has-error @endif">
<input type="hpnum" class="form-control" type="hidden" id="mobile" name="mobile" placeholder="Enter handphone number">
@if ($errors->has('mobile')) <p class="help-block">{{ $errors->first('mobile') }}</p> @endif
</div>
</div>
<!--<div class="form-group">
<label class="control-label col-sm-3" for="officeEx">Office Extension:</label>
<div class="col-sm-5">
<input type="officeEx" class="form-control" id="officeEx" placeholder="Enter office extension">
</div>
</div> -->
<div class="form-group">
<label class="control-label col-sm-3" for="role_id">Role:</label>
<div class="col-sm-5">
<select class="form-control" type="hidden" id="role_id" name="role_id">
@foreach ($roles as $role)
<option value="{{ $role->id }}">{{ $role->role_description }}</option>
@endforeach
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-5">
<button type="submit" class="btn btn-default">Update</button>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
Controller.PHP这样
class ManageAccountsController extends Controller
{
public function index()
{
$users = User::orderBy('name')->get();
$roles = Role::all();
return view('manage_accounts', compact('users', 'roles'));
}
public function store(StoreNewUserRequest $request)
{
// create the data for new user
$user = new User;
$user->name = Input::get('name');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->mobile = Input::get('mobile');
$user->role_id = Input::get('role_id');
// save new user
$user->save();
Session::flash('flash_message', 'User successfully added!');
return redirect()->back();
}
public function update($id)
{
// update existing user
$user = User::findOrFail($id);
$user->name = Input::get('name');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->mobile = Input::get('mobile');
$user->role_id = Input::get('role_id');
// save existing user
$user->save();
}
}
解决方法:
所有resource controllers为您提供的便捷快捷方式如下:
Route::get('/resource', ['as' => 'resource.index', 'uses' => 'ResourceController@index' ]);
Route::get('/resource/create', ['as' => 'resource.create', 'uses' => 'ResourceController@create' ]);
Route::post('/resource', ['as' => 'resource.store', 'uses' => 'ResourceController@store' ]);
Route::get('/resource/{resource}', ['as' => 'resource.show', 'uses' => 'ResourceController@show' ]);
Route::get('/resource/{resource}/edit', ['as' => 'resource.edit', 'uses' => 'ResourceController@edit' ]);
Route::put('/resource/{resource}', ['as' => 'resource.update', 'uses' => 'ResourceController@update' ]);
Route::delete('/resource/{resource}', ['as' => 'resource.destroy', 'uses' => 'ResourceController@destroy' ]);
因此,如果您调用Route :: resource(‘manage_accounts’,’ManageAccountsController’),您将创建7条路线.你特别告诉Laravel只创建其中的3个,即:
Route::get('/resource', ['as' => 'resource.index', 'uses' => 'ResourceController@index' ]);
Route::post('/resource', ['as' => 'resource.store', 'uses' => 'ResourceController@store' ]);
Route::put('/resource/{resource}', ['as' => 'resource.update', 'uses' => 'ResourceController@update' ]);
您可以通过请求路由URL来控制控制器上的这三种方法,例如:你只需要求它的URL就可以“调用”索引路径:
GET http://server/resource
<form class="form-horizontal" role="form" method="POST" action="/manage_accounts" novalidate>
这将在提交表单时创建此HTTP请求:
POST http://server.com/manage_accounts
将其与资源路由进行比较,最终会调用控制器的store()方法.如果您希望该表单调用update()方法,则必须发出PUT请求.由于HTML形成can’t make PUT requests,Laravel提供了一种模式PUT请求与表单的方法:
<form class="form-horizontal" role="form" method="POST" action="/manage_accounts/{{ $account->id }}" novalidate>
<input type="hidden" name="_method" value="PUT" />
另请注意,表单发布到的网址已更改,并应包含您要更新的实际帐户的ID.
您可能还会发现将此与工匠本身视为可用路线进行比较会有所帮助.您可以通过发出artisan命令列出所有可用路线$PHP artisan route:list(Laravel 5)或$PHP artisan routes(Laravel 4)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。