# Keeping track of how an action was run

# The runningAs method

In some rare cases, you might want to know how the action is being run. You can access this information using the runningAs method.

public function handle()
{
    $this->runningAs('object');
    $this->runningAs('job');
    $this->runningAs('listener');
    $this->runningAs('controller');
    $this->runningAs('command');

    // Returns true if any of them is true.
    $this->runningAs('object', 'job');
}

# The before hooks

If you want to execute some code only when the action is running as a certain type, you can use the before hooks asObject, asJob, asListener, asController and asCommand.

public function asController(Request $request)
{
    $this->token = $request->cookie('token');
}

If you want to prepare the data before running the action (and thus also before validating the data), you can use the prepareForValidation method. This method will be run before the as... methods, no matter how the action is running as.

TIP

It is worth noting that, just like the handle method, the prepareForValidation method and the as... methods support dependency injections.

TIP

The page "The lifecycle of an action" provides a handy summary of all methods that an Action will call before and after executing the handle method.

WARNING

These before hooks will be executed at every run. This means you cannot use the asController method to register your middleware. You need to use the middleware method instead.

Last Updated: 1/26/2022, 11:46:22 AM