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
24. Page titles in Twig

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 recap by going through the entire flow of creating a new route, new controller and a new
00:05
view and then we'll look at changing over the page title depending on which page we're actually on. So to start with let's head over to our web routes and we'll create our new route in here for the user's dashboard. So let's create dashboard. Eventually we're going to be protecting this so the user can only access it when they're signed in but for now we'll just
00:25
register it as a plain route. Okay we need a controller here so why don't we head over to HTTP controllers. We could even grab the same structure as our home controller just to make life easier and let's create out a dashboard controller file in here, paste this in and of course just change over what we need. So let's call this dashboard controller. We don't need
00:49
our config for this but we do need the ability to render a view and in here we'll get rid of users and name and we'll just render out dashboard.twig which we'll create in just a second and we don't need any data to be passed down to here at least not yet so let's get rid of that as well. Okay over in our web routes we can now pull the namespace in for our dashboard controller so
01:10
let's pull that in and get rid of that and that's been pulled in just at the top here. Great. Okay so we have a route, we have a controller but we don't have a view. If we were to go over to our dashboard yeah sure enough we don't have dashboard.twig. Okay let's first of all add this to our navigation so I'm just going to create out another navigation item here which we'll be hiding when
01:34
the user isn't signed in so we can now click through to that directly from here. Okay all we need to do now is create out a new view so we could put this in a folder if we wanted to it doesn't really matter how we do this let's just create this out directly in here so dashboard.twig and paste this in let's go and just get rid of this and call this dashboard and we'll get rid of this as well. Okay
02:03
and we'll just say this is your dashboard. Okay brilliant so we're extending the base layout everything should look good like this. Perfect. Now we want to change over this page title here so how do we do that? Well we can use the same concept as a piece of content that we want to inject into a view so if we open up home.twig what we can do is as well as defining this block out
02:29
here we can also define a block in here for the title we're going to do this inline because we don't want any extra spacing so we'll end that block and we'll give the title in here and let's do the same thing for dashboard as well so let's pull this in and we'll call this dashboard. Okay so obviously this isn't enough on its own what we'll need to do is head over to our base layout
02:55
so layouts and app and we'll need to inject this into here under the condition that it actually exists. To do this we want to check if the block is actually defined first of all so let's use a if statement in twig and let's use the block helper here to grab the contents of the title block what we can then do is say is defined so if that block is defined we want to do something in here
03:19
we need to end that if here as well and this is where that content is going to go so what we can now do is just directly in here render out that title and let's go over and have a look there we go we've got home and we've got dashboard now what we could also do is onto the end of this add in the name of the app the overall name of the app and we'll change this up later when we create a
03:44
config helper specifically for twig to actually pull this out of our config but now though we know that we need to hard code it in so we'll look at changing this over later to do something like the following config get and then grab out any of the config that we need okay so we'll add no framework into here if the block is defined we'll add just a pipe at the end of this to separate
04:09
these two things out and now we have dashboard pipe no framework and home pipe no framework now if we don't have a block defined here that's fine because we're using an if statement so for example on the home page if you didn't want to specifically say home that's now just going to show the name of the app and on any of the other pages it will render out properly
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!