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
53. View and config helpers

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 just add a couple more helpers to our helpers file and then I'll let you go ahead and add any more if you need to.
00:07
So the first one and probably one that's going to be used the most often if we head over to our controller is going to be to render out a view. Now what we're doing at the moment is we are saying this view render. So we're pulling this dependency into our controller and we're saying this view render, passing everything in and then passing any data we want to send out. It would be really convenient if we could just use view to create out a view for us and then write it to the body.
00:35
What we could even do is go a step further and create a response and return that from the helper function. Let's look at doing this first and then we'll swap it over. So over in our helpers, let's go and create out a new function in here called view. Now we know that we need to pass in all of the potential data that our view needs.
00:55
So we're going to have a view in here and we're going to have an array of data that we want to send down to here. And then it's just as easy as going ahead and grabbing our view out of our container. Now, since we've already got our app helper, that's as easy as just saying app view and then render and then just passing this data directly down. Once we return that, that is all we need to do.
01:20
This is just going to work in exactly the same way. But it looks a lot cleaner over in our controllers. If you wanted to take this a step further, what you could do is you could take the response and you could take all of this data and return it. So you would only end up really with one line when you wanted to do this.
01:39
Let's go ahead and move that over now and I'll let you decide if you want to do this. So in here, let's just paste everything in that we took out of there. Let's still return this response. And in here we will go ahead and grab from here what we were returning before.
01:55
So we're basically just swapping over, creating a response in here, writing to the response and then returning that response. Again, using our app helper to render everything that we need. So now all we need to do in our home controller is get rid of all of this code and return a new view. So let's just grab what we are doing here.
02:15
Get rid of all of this. And then go ahead and return this directly, and that will work nicely. And of course, it's tidied everything up. You don't need to do this.
02:27
So I'll leave the rest of the controllers in the standard way where we're creating a response from writing to it. But you can change this over if you need to. So as the last example, I'll let you add more if you want to. Let's create out a config helper.
02:40
So let's go back over to our helpers here and let's grab out a config function. And we know that to get config, we need to pass through a key. And then potentially we need to pass through a default. By default, we'll set that to null.
02:57
Now, once again, we're going to do exactly the same thing. Use our app helper to grab config out of our container. And then we'll just say get key and default. And remember, we built this config class up earlier, so feel free to take a look at it if you need a reminder.
03:13
Okay, now we've got this. We know that we can just use this anywhere. We know that we were using our config helper a little bit earlier over in things like our view service provider. Let's just find that again.
03:25
And let's open this up and have a look. So rather than do all of this stuff here, debug and fetch this out of the container, then get app debug, we can just switch this directly over to config app and debug. And that makes it a lot shorter, cleaner, and we can even get rid of this temporary variable.
03:45
Put this directly on here and everything looks a lot neater. Again, I'll just leave this as an example and I'll let you go ahead and change this over if you want to. Okay, so it looks like this isn't working. So let's just double check what we've done here over in helpers.
03:58
And yet, sure enough, we set this as a public function, which, of course, is not right. So give that a refresh. Everything's working as normal. I'll let you go ahead and create as many of these as you want if you would like to. Just makes it a lot nicer to write our code.
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!

Comments

No comments, yet. Be the first to leave a comment.