简体   繁体   中英

Before executing Action in Laravel 5.1

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);
    }
}

2020 update:

If you find yourself reusing validation rules in Laravel 5+, I recommend adding them to a request:https://laravel.com/docs/6.x/validation#form-request-validation

/**
 * Store the incoming blog post.
 *
 * @param  StoreBlogPost  $request
 * @return Response
 */
public function store(StoreBlogPost $request)
{
    // The incoming request is valid...

    // Retrieve the validated input data...
    $validated = $request->validated();
}

A built-in solution would be to use middlewares, however I see that you would like to execute that piece of code for specific actions.

If I were you, I'd create a concrete controller class that all my controllers would inherit from, and this controller would look something like this:

class FilteredController extends BaseController
{
    private function getControllerAction(Request $request)
    {
        $action = $request->route()->getAction();

        $controller = class_basename($action['controller']);

        list($controller, $action) = explode('@', $controller);

        return ['action' => $action, 'controller' => $controller];
    }

    private function validateUpdateRequests()
    {
        /* Validation code 
        that will run for update_post action, 
        and update_post ONLY*/
    }

    public function validateCreateRequests()
    {
        /* Validation code that will run for 
           create_post action, and 
           create_post ONLY.*/
    }

    public __construct(Request $request)
    {
        $route_details = $this->getControllerAction($request);

        if($route_details['controller'] == 'postsController')
        {
            if($route_details['action'] == 'update_post')
            {
                $this->validateUpdateRequests();
            }

            else if($route_details['action'] == 'update_post')
            {
                $this->validateCreateRequests();
            }
        }
    }
}

I hope this helps.

Again , the better way would be using middlewares, and to use middlewares for specific actions, you'll need to specify the filtering in the routes, such as :

Route::get('/route_to_my_action', 'MyController@myAction')
->middleware(['my_middleware']);

For further information about this style of filtering in Laravel, visit the docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM