This episode is for members only

Sign up to access "Build Your Own PHP Framework" right now.

Get started
Already a member? Sign in to continue
Playing
19. Using services inside controllers

Episodes

0%
Your progress
  • Total: 4h 45m
  • Played: 0m
  • Remaining: 4h 45m
Join or sign in to track your progress

Transcript

00:00
Let's say that we wanted to shove our app's name on the home page rather than just a plain piece
00:05
of text. How do we do this? Well we know that within our container we've set up auto wiring and we know that we have over in providers our config service provider which will give us back our app name. This is registered under config over on the container. Now because we've got this available what we can do is we can just create our constructor. We looked at auto wiring earlier
00:29
how we could resolve dependencies. Because this home controller is just a class this means that in the constructor we can just access whatever we need out of the container. So for example we could access our config directly from our container and we'll call that config and then that's now available to us. So we don't need our block here so let's just get rid of that and let's go and
00:53
access our config in here. So let's write to the body from config and we will get app and name. Let's head over give that a refresh and there we go we now have the name of our app displayed on the home page. Now this works because over in our web routes when we pass in our home controller in here our router will resolve our home controller out of the container even though we've not
01:19
specifically added it to the container. And because in our home controller we have a constructor which relies on our config this will be resolved out of the container as well and it will be available to us in a protected property called config which we can then access down here. So throughout the course we're going to be adding a lot to our constructors. Well not too much but
01:42
everything that we need in our constructor. For example if we want to construct a view and write that to the body we will pull out our view class to render out a view. But there we go that's how easy it is to grab anything out of our container within a controller that we can use within our controller invoke magic method.

Episode summary

In this episode, we walk through how to use services inside controllers in our application. Instead of hard-coding values like your app's name directly into your homepage, you'll see how to leverage your container's setup—to fetch configuration details and use them wherever you need.

We revisit the container and see how services (like our config provider) are registered and made accessible. Using constructor dependency injection and the power of auto-wiring, we demonstrate how you can pull dependencies, like config, into your controller. From there, it becomes super easy to output your app name (or any other value from your config) right onto the homepage.

By the end of the episode, you'll understand why this method works: when the router resolves a controller via the container, it automatically wires in constructor dependencies. This makes everything clean, reusable, and testable. Plus, as your app grows, you'll see how you can add more services, like a view renderer, by pulling them into constructors in the same way.

In short, you learn how to access anything you need from the container inside your controllers, making them a lot more powerful and flexible!

Episode discussion

No comments, yet. Be the first!