【发布时间】:2015-12-17 08:56:22
【问题描述】:
你好,我是 laravel 4.2 的新成员,我正在控制器中创建一个应用程序,我创建了自己的函数 c_updateSystemUser($id),这是代码
public function c_updateSystemUser($id)
{
//
session_start();
$getsCapt = $_SESSION["captcha"];
$rules = array(
'username' => 'required|min:2|max:50|regex:/^[a-zA-Z0-9\-\s]+$/|exists:dbo_systemusers,SystemUserName,$id',
'description' => 'required|min:1|max:100|regex:/^[a-zA-Z0-9\-\s]+$/',
'usertype' => 'required|numeric',
'capt' => 'required|numeric'
);
$validator = Validator::make(Input::all(), $rules);
// process the inputs given by the user
if ($validator->fails())
{
return Redirect::to('viewsu/' . $id)
->withErrors($validator)
->withInput(Input::except('password'));
}
else
{
// store the values into the database
$su = SystUser::find($id);
$su->SystemUserTypeID = Input::get('usertype');
$su->SystemUserName = Input::get('username');
$su->SystemUserDescription = Input::get('description');
$su->timestamps = false;
$su->save();
Session::flash('message', 'Successfully created system user!');
return Redirect::to('viewsu');
}
}
}
我希望在用户单击我的视图文件上的更新按钮时调用此函数,但是当单击该按钮时,它会重定向到其他页面(我的创建用户页面)我不知道为什么 这是我的刀片中的表单代码
{{ Form::model($su, array('route' =>array('csu.update',$su->SystemUserID),'method'=>'PUT'))}}
<div class="form-group">
{{ Form::label('usertype', 'User Type') }}
{{ Form::select('usertype', [null=>'Please Select user type'] + $sTyp , array('class'=>'form-control'))}}
{{ Form::label('current' , 'Current Type: (unedited) '.$su->SystemType , array('class' => 'label label-primary')) }}
</div>
<div class="form-group">
{{ Form::label('username', 'Username') }}
{{ Form::text('username', $su->SystemUserName, array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('description', 'Description') }}
{{ Form::textarea('description', $su->SystemUserDescription, array('class' => 'form-control','rows' => '3')) }}
</div>
<div class="form-group">
{{ Form::label('captcha', 'CAPTCHA image: ') }}
</br>
{{ HTML::image('http://localhost:8080/laravel3/app/captcha.php', 'alt' , array( 'width' => 200, 'height' => 35 )) }} </br></br>
{{ Form::text('capt', Input::old('capt'), array('class' => 'form-control','placeholder' => 'enter generated captcha')) }}
</div>
{{ Form::submit('Edit System User', array('class' => 'btn btn-primary')) }}
{{ Form::close()}}
我不知道如何让表单在我的控制器中调用函数public function c_updateSystemUser($id)
我的控制器文件名为 systemUsers.php
有什么想法吗?任何帮助将不胜感激
【问题讨论】:
-
你能发布你的路线吗?
-
Route::resource('csu','systemUsers@c_storeSU'); Route::get('createsu', 'systemUsers@c_createSystemUser'); Route::get('viewsu','systemUsers@c_viewSystemUser'); Route::get('viewsu/{id}' , array('as' => 'findSU', 'uses' => 'systemUsers@c_editSystemUser'));这是我使用的路线 -
您没有处理 put 请求的路由?
-
你会想要这样的东西:Route::put('/doesntmatter', array('as' => 'csu.update', 'uses' => 'myController@c_updateSystemUser'));
-
另附注,如果我是你的老板并且知道 laravel ......我不会接受它。请使用 laravel 文档和 PSR 标准提供的命名约定。例如,没有 c_updateSystemUser... 而是创建一个用户控制器,然后有一个 update(User $user) 方法,其中一个用户绑定到传递的参数。您还可以使用表单请求验证:laravel.com/docs/5.0/validation#form-request-validation
标签: php laravel-4 laravel-routing