在Laravel 5.1中执行Action之前 [英] Before executing Action in Laravel 5.1
本文介绍了在Laravel 5.1中执行Action之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在存储和更新方法中编写以下代码:
I am writing the below code in store and update method:
$v = Validator::make($request->all(), [
'field' => 'required|max:100|min:5'
]);
if ($v->fails()) {
return redirect('route name')
->withErrors($v)
->withInput();
}
在执行任何动作方法之前,是否有执行的内置动作方法?如果是这样,那么它对单个操作方法还是对控制器有效?
Is there any inbuilt action method that executes before executing any action method ? if so, is it valid for individual action method or for the controller?
推荐答案
You may use a middleware or override callAction
, https://laravel.com/api/5.6/Illuminate/Routing/Controller.html#method_callAction
use Illuminate\Routing\Controller;
class MyController extends Controller
{
public function callAction($method, $parameters)
{
// code that runs before any action
if (in_array($method, ['method1', 'method2'])) {
// trigger validation
}
return parent::callAction($method, $parameters);
}
}
这篇关于在Laravel 5.1中执行Action之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文