Php 如何在Laravel 5.3中的一个控制器中使用不同的表单请求
我是Laravel的新手,我正在尝试创建两个不同的表单请求,以便在同一个控制器上使用它们 其中一个将在创建和编辑模型之前验证我的表单。(AngendaFormRequest) 另一个将验证我的AJAX操作。(AgendaAJAXFormRequest) 当我尝试使用AJAX删除事件并调用AgendaAJAXFormRequest作为参数时,我的请求会一直通过AgendaFormRequest方法传递 如何阻止这种行为 以下是我的应用程序设置: 路线: 代理请求 AgendaAJAXFormRequest 在控制器上删除和编辑方法: Javascript请求(jQuery)Php 如何在Laravel 5.3中的一个控制器中使用不同的表单请求,php,ajax,laravel,laravel-5,laravel-5.3,Php,Ajax,Laravel,Laravel 5,Laravel 5.3,我是Laravel的新手,我正在尝试创建两个不同的表单请求,以便在同一个控制器上使用它们 其中一个将在创建和编辑模型之前验证我的表单。(AngendaFormRequest) 另一个将验证我的AJAX操作。(AgendaAJAXFormRequest) 当我尝试使用AJAX删除事件并调用AgendaAJAXFormRequest作为参数时,我的请求会一直通过AgendaFormRequest方法传递 如何阻止这种行为 以下是我的应用程序设置: 路线: 代理请求 AgendaAJAXFormReq
这通常会导致错误。这里有一些建议 把你的
Route::delete('/delete','ConcertController@delete');代码>上面的路线
Route::get('/{id}', 'ConcertController@showRegistrationForm');
Route::post('/{id}', 'ConcertController@edit');
如果这不起作用,那就试试看
$.ajax({
url: href, // /agenda/delete
type: 'DELETE',
data: {
id: id, // event id
_token: window.Laravel.csrfToken,
},
})
你能展示你的ajax和html代码吗?我添加了Javascript请求,并对chrome Debugger上检查的变量值进行了注释。尝试删除验证并检查它是否使用了正确的方法。比如:公共函数delete(Request$req){return“test”}即使我尝试使用标准的Request类,它也会在AgendaFormRequest validationThank上返回一个错误!改变路线顺序成功了!你知道是什么原因吗?那是因为你在发送post请求。laravel正试图通过这个路径::post('/{id}','ConcertController@edit'); 其中id=delete。但当您更改位置时,laravel首先检查route::delete('/delete','ConcertController@delete');. 对不起英语。如果你不明白,我可以解释得更清楚。我明白了!再次谢谢你!不客气!)你能把它标为正确答案吗?
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class AgendaAJAXFormRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'id' => 'required'
];
}
/**
* Get the error messages for the defined validation rules.
*
* @return array
*/
public function messages()
{
return [
'id.required' => 'O id é obrigatório nesse tipo de requisição.',
];
}
}
public function edit($id, AgendaFormRequest $req){
$data = $req->all();
try{
$concert = Concert::findOrFail($id);
} catch(\Exception $e) {
return redirect()->back()->with('err', 'Evento não encontado');
}
$validators = [
'time'=> 'date',
];
$this->validate($req, $validators);
$concert->update($data);
return redirect($this->home)->with('alert', 'Evento editado com sucesso!');
}
/**
* Delete the event of id = $id
* @param int $id
* @return string
*/
public function delete(AgendaAJAXFormRequest $req){
try{
$concert = Concert::findOrFail($id);
} catch(\Exception $e) {
return 'Evento não encontado';
}
$concert->delete();
return "Evento $id deletado com sucesso!";
}
$.ajax({
url: href, // /agenda/delete
type: 'POST',
data: {
id: id, // event id
_token: window.Laravel.csrfToken,
_method: 'DELETE'
},
})
Route::get('/{id}', 'ConcertController@showRegistrationForm');
Route::post('/{id}', 'ConcertController@edit');
$.ajax({
url: href, // /agenda/delete
type: 'DELETE',
data: {
id: id, // event id
_token: window.Laravel.csrfToken,
},
})