In this episode, we're diving into how you can use actions within other actions. Up until now, all our actions have been pretty straightforward, each doing a single thing—like creating an order, creating a post, or sending an email. That's the standard approach, but it's totally fine to call one action from another, and that's what we're exploring.
We start by setting up a new action called DoSomething
, just to keep things simple. Then, we create another action that fetches some data—imagine this could be from an API, like pulling plans from Stripe, or any other kind of external data fetch. Inside our DoSomething
action, we use this new "fetch" action to grab some data and then do something with it. The example is pretty stripped-down, but it's easy to see how you could expand this for a real-world scenario.
There's more than one way to call actions within actions. You can either call an action's run
method directly where you need it, or use dependency injection to pull actions in via the constructor—both approaches work, and we see examples of each. Which one you'll use can depend on your needs, especially when it comes to testing (something we'll cover soon).
One thing to keep in mind: while actions can interact with each other, it's important not to overload a single action with too much responsibility. If you start doing too much inside one action, that's a hint to break things up for better maintainability and clarity. For now, fetching some data in one action and then processing it in another is a perfectly fine use case!
By the end of the video, you'll get a clear sense of how to structure your actions to keep things simple, flexible, and easy to test.