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.
54 episodes4 hrs 45 mins

Overview

Starting completely from scratch, build a modern PHP framework with all the features you’d expect.

Whether you’re new to PHP or not, this is a great exercise for learning what happens under the hood, arming you with knowledge you can apply anywhere you use PHP.

We’ll cover routing, controllers, views, the container, accessing the database, models, authentication, config, CSRF protection, exception handling, pagination, validation, flashing messages and much more.

Let’s dive in and build a PHP framework, step-by-step!

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!