 I'm gonna be loud anyways but for the recording so we have you guys can watch this later or force your friends to watch this So hello, so now we are going to start so finally if you're coming in at the end now You don't get a seat you have to sit back sit back there by yourself. My name is Ryan Let's see if my clicker works. It does awesome. Hello Hold on a second. That doesn't look right. There we go. Welcome to New Orleans. This is probably my favorite city in the US Austin is like my second favorite. So like Drupal cons been doing really well kind of just forcing me to go to these amazing US city So I'm really excited to be here The quick party get to talk about me. I come from the symphony world, which this is my fourth Drupal con Coming from the symphony world So if anybody is benefiting more from like the symphony Drupal marriage is like then me I haven't met them Like I get to come to this conference It's like probably my favorite conference to talk at actually because of you guys and this is the sounds like something that everybody says But really the people at Drupal con have just a different energy level and curiosity than other conferences So very happy to be here. My day job. I'm a writer for a camp university calm We do PHP symphony and screencasts and puns And coding challenges. How many people have heard of us before? That's not bad. Okay. Yeah, that's not bad like I would say that's like 40% of the room 30% of the room So like not doing that bad of a job at marketing, but not doing that great of a job So check us out after this. I'll have a couple links at the end and most importantly There she is way there she is I am the husband of the much more talented Leanna Palin who's the true creator of the puns You can see it because on her name card. It says that's her job at camp University pun creator Oh, let's see here. What can we do with Leanna? Well, let's go with the classic thing Leanna. She loves high fives. So if you want to get a good look at her back there She's definitely looking for some high fives No, it's not her. They look kind of alike. It's the one in green back there She likes high fives. So if you can give her high five afterwards, you'll basically be her best friend She's super friendly way more friendly than I am Or you can you can tweet at her and harass her through this that would be cool, too All right, so let's get into the stuff. We actually want to get into so the fundamentals of every framework ever Except for Drupal, but not anymore. Yay so a big thing with the Drupal the new Drupal thing is is is You know Drupal's taking a step towards what everybody else is doing Which is which has a lot of benefits. That's part of what this presentation is about you guys are going to Accidentally even if you don't care about symphony Just by doing Drupal stuff and getting better at Drupal 8 you're going to accidentally unlock a whole other set of tools So let's look at Drupal 7. How many people you guys remember this hook a menu, right? cool And there's laughing but the new system ain't so far off from this it will look different but fundamentally It's the same. I'm not even going to explain it. You guys know how come on you so Let's talk about How because this would be a very simple way to create a custom page in Drupal So how would you create a custom page in symphony? Well, the key things we're going to talk about a routes and controllers requests and responses and service container Let's talk about those all in a second. The most important thing is Everything I'm going to talk about because I'm going to show you things in symphony And I'm also going to show you things in Sylex, which is a micro framework You'll see in a second everything we're going to talk about is going to be the same in Drupal In fact, most of the slides you could copy and paste into a Drupal module and they would just work I mean, that's amazing. This is why I get to talk at a Drupal conference because there's so much that's shared between the two So this is Sylex how many people have heard of Sylex before It's pretty good. How many people have used Sylex decent bit. Okay. Yeah a few last hands So Sylex is a micro framework. So the entire application is right here So we have six lines of code literally one file you spin up a web server pointed at this file and you're good to go So an entire application that says hello, and we're going to walk through the parts of this because as simple as it seems this embodies Basically 50% of the the kind of new stuff that came in under the hood with symphony so Sylex the reason I talk about Sylex, I realize this is important is Sylex is also built on the symphony components. So Drupal is built on the symphony components This little guy here is built on those same components. So they're all not cousins of each other more like siblings of each other So if we are going to write this we literally open up a file we write this of course You have to grab Sylex via composer But you know assuming you have that in this directory as well you start up this file then we need to point a web server at It's I'm lazy. So I never do that anymore. I use the built-in web server How many people have used the built-in PHP web server at this point? That's that's getting to be more and more hands. So for those of you that haven't it's not something that you have to use I do a lot of training so it's like the trainer's best friend ever because I have to I don't have to debug people's Apache virtual hosts anymore Thank God So basically you can just pop into any directory on your machine in command line Just run PHP dash capital s localhost colon eight thousand and boom you're serving at localhost colon eight thousand everything from that directory so I Actually use this for most of my projects if you have if you need to set up SSL custom subdomain stuff like that You're gonna need a real web browser, but this works pretty darn. Well, I don't use it on production though Just by the way, like he told me to it worked pretty well. No, it's it's it's very very slow by Production standards that it's fine for development cool. So then that's it. So I guess Localhost colon eight thousand pretend that's like eight thousand one I just realized my next slide says localhost colon eight thousand one. Oh, there it is cuz up there Yep, so I started at PHP dash capital s localhost colon eight thousand one and that's it Then I go to slash hello slash Drupal dash people and it screams at me down here So cool. So here are the the fundamental pieces that we need to talk about so a request comes in It says I want slash hello slash Drupal. That's the URL Then the routing layer its job is to determine Actually, that's let me say that a different way Well, that's actually true. Oh, I'll break this on my piece a second The job of the routing is to figure out which function should be called for that page If you think of hook menu, it's kind of that first part where you're kind of defining what the URL looks like That's basically the route Now the route is going to point to a controller in the same way that hook menu pointed to a function a Controller is just a function that builds the page controller is an overloaded word. It's a function that builds the page That's it. So the routes going to point to the controller the controller is going to be what we write to build the page And that's it. So really think of like hook menu being these two pieces. You're like, yeah, that's that's the same thing We're just we're just naming it to different now So that's easy and then our only job in the controller is to return a response object So symphony comes with a response class. You're going to create a new response object throw some content into it And that's what you'll return from your controller So looking at that really really simple Sylex app Let's break down the pieces. So first of all this is obviously the route So the route is basically just the URL pattern and in symphony Drupal Sylex, you know, it's all the same When you have a curly brace anything curly brace, that's just a wild card So you'll kind of see that in a lot of different frameworks and things So that's what it looks like inside of symphony. So this matches slash. Hello slash anything now The controller so if the UI matches this route the URL that's coming in matches this route and Sylex executes This anonymous function, that's the controller. So route points to the controller very simple And as an added benefit any of your curly brace things are passed as arguments to your controller function And that's done by the name of it. So the fact that there's curly brace name points to dollar sign name That was curly brace foo. It would be dollar sign foo So it's just done by matching on name and then we write the controller and we build a page and that's it So again request comes in it matches the route route points to a controller control as a function that we build That we write that builds the page cool So that was easy. So let's see how that looks inside of symphony. So symphony is a little bit bigger But it's gonna have the same fundamental philosophy. Oh drosh Drupal console. So I scream those because symphony has something just like drosh or Drupal console I know that that's a hot topic right there like frenemies drush and Drupal console We only have one of them. So it's a life is good on our side and actually our Version of this is actually just an installer So when you go to install symphony, you will actually install something like drosh or Drupal console It's a global executable But actually its only job is to just help you download symphony not that downloading symphony is hard But we found out that if we give people an installer then after they download it We can give them this really great message about how great they're doing and like what to do next and like very encouraging Messages and we color everything with green font and it helps them get along to that next step So this is just a glorified downloader. So you'll see this when you install symphony And this is the green text. I was talking about successfully installed with a check mark So you just say symphony new and then you give it a directory name It literally is just downloading a symphony project into that directory name So you guys are all very familiar with that because that's the same way basically you get Drupal You maybe get it with Drupal console or you just download it and unzip in a directory. So same thing here And this is what it looks like and the cool thing about a a symphony project is there's not much in it Very few files. I think if you commit a stock symphony project, it's somewhere like counting fave icon and all that It's somewhere around 30 files or so. So it's very very small so the one kind of key difference between The Drupal structure for things and the symphony structure for things is in Drupal You always create a custom module and you put everything inside of that module Maybe you have many modules symphony also has this idea. It's it's almost the same exact thing They're called bundles that you can see this app bundle there and when you're creating a contrib Bundle, it's very very similar like you put everything inside of this bundle and then you share it with all of your friends However, when you're writing code that goes in your application We actually purposefully break from this model and you actually put the code for your application Kind of like right more more right at the root of your project So you'll kind of notice that difference, but it ends up being simple because you have an app directory Which can canes configuration templates or said better PHP classes go in the src directory Everything else goes into the app directory So if I tell you to like make a YAML file you're gonna be like that's somewhere in the app directory that I need to do that So this is what I want you to think about When you're kind of thinking about the structure app and source they can are gonna contain 99% of your code We also have a var directory that's cache logs stuff like that So right there'd be like sites slash default slash files or whatever, you know It's just stuff that you don't have to worry about that's being cached inside of there vendor third-party code That's just like Drupal 8.1. There's that vendor directory. So that's no difference and the web directory is actually the document root So that's like one small difference instead of the root of our project being the document root It's that web directory. So you point your web server right there Cool triple console Symphony console, that's right. I was like, why was I screaming Drupal console at this point? Symphony console so I mentioned that we have that symphony installer which just downloads symphony and that's all it does But once you get into a symphony project we actually ship with Basically the our version of the console that does everything else So the global one just downloads it but as soon as you move into that directory you have a bin slash console file It's actually physically a file. There's a bin directory with a file called console in it It's executable. It's just a PHP file and you can use that to run stuff So just kind of think that's gonna be kind of running Drupal console or drush and giving you a list of commands that you can run in Your project and this directory And one of them is server colon run Again Drupal console has something just like that. It's called Drupal space server And it's just a shortcut to spin up that built-in web server not that that was hard But it does it for you and it's smart enough to know that in symphony the web Directories your document root so it kind of you know points your web server at that directory But you could have done it yourself And then wow there it is. So that's it. So we just started up our built-in web server I go to local host colon eight thousand and boom we have a brand new Symphony project and again we try to give you something that feels very encouraging so that you want to continue on to the next step Isn't so much of kind of open source. It's like it's marketing right developer marketing feels good again theme We're keeping a consistent theme with the green checkbox. You see green checkbox. It means you're probably making progress This is a good thing You're seeing this actually because because remember like symphony is going to follow that same route controller model You actually in your project this project is basically empty to start with but we do actually give you one route and one controller That renders this page So this is actually coming from a real route and controller in your system and check this guy out down here I'm going to talk more about this later. This is symphonies web debug toolbar. This is like super killer awesome feature It's contains a bunch of debug information. I'll talk about later the most important thing to know about now is We redesigned it to be very black and flat and cool and hipster So if you need to make like a hipster based argument to management to use symphony This is this is where you want to go for it looks very very cool down here, okay? All right, okay cool So we are at the home page right now local host calling 8,000 because that's where that one routes points to So if I go to any other URL like slash nothing to see here Then you're going to get this page because there's no route for that page So that's what you see here. No route found for you know slash nothing to see here Which is basically symphonies way of saying hey, I'm the symphony Pac-Man ghost and they you know things are okay But there is no route here yet okay, or If you install a certain bundle called the Jolly code gif exception bundle you will see one of various random gifts explaining the situation in a more more visual way Cool, so we've installed symphony Which is kind of basically one command. So that's cool. So let's get to the important stuff of building a page So we know we need two pieces. We need a route and we need a controller Okay, so routing is done. Well everything in symphony can be done in like 500 different formats Symphony is like so flexible. You might you know, that's you can you can do anything even kind of hurt yourself potentially But anyways, usually it's done in a YAML file although that's a lie And I'll tell you why I'm lying in a second, but just you know kind of eat my lie for now So yeah, okay. Everything's done in a YAML file. So typically routing is done in the YAML file and oh oh in and so Where does that YAML file live? Well, it's not a PHP class, right? So it probably lives somewhere in the app directory So it actually lives an app slash config slash routing that YML So remember those two pieces to the route first we have to define the URL So there's our slash hello slash curly brace name and a second thing We need to do just like hook menu is we need to point to the function the controller That's going to build the page by the way this hello underscore world here. That's a machine name for the route It's not important now. It will become important later if you want to like refer to this page refer to this route link to this route something like that So there's our path and the way that you point to the controller Which is the exact same inside of Drupal is by doing this underscore controller thing now Obviously, this is not the real value the value looks like this. I just ran out of room on that line You know because there's not that many characters on a slide So the difference between Well, let me say it this way in Sylex, right? Our controller was actually an anonymous function in Symphony it can be anything but it's typically a method inside of a class and so same thing in Drupal It could be anything symphony really doesn't care You just got to give it something it can call and it's like far out I'll call that function as long as it returns a response. I'm happy But it's usually a function side of a class. We have not created this class yet But I'm using namespaces here. Obviously another kind of new concept But here's my long class name, and then it's just colon colon and then the method name We're gonna have inside of that so cool. So let's create that so let's say that's a php class So that lives in the src directory and in fact This could live this controller class could live anywhere in the src directory. It's typically an app bundle slash controller But you can put it anywhere the only rule and this is true with Drupal as well Is that your namespace matches your directory structure? So the fact that I have namespace app bundle slash controller means I need to be in the app bundle slash controller directory if I change that namespace I could move it into the corresponding directory and and update the routing file and life would be good Okay, and that's the same thing in your custom Drupal 8 modules There's a slight twist to that rule But it's the same thing there's an src directory in your modules And then you're gonna have to move things around just based on what namespace you're giving it And you can give usually you can give things whatever namespace you want Usually there's some standards you'll follow so that you don't create the most horrific Frankenstein project ever But technically speaking it usually doesn't matter where you put things so cool. So we have a class here Here's our functions say hello action. I'm passing the curly brace name So we're doing the dollar sign name thing here And as I said our only job with the controller is to return a symphony response object So this is the use statement for the response object and I mean response objects are easy Responses are nothing more than content headers status code. So response. I give it content It defaults to 200 and it has a couple of really basic headers by default So that's it. So it's the same exact model Just done in a class And we can pull it up that way Cool So again request routing controller response Across now we're across a Drupal Silux and symphony, which is pretty cool because those are three very different beasts They all have the same fundamental philosophy So debugging so let's see here if I handed you a big symphony project or a big Drupal project You might be like what pages do I have or you might go to a certain URL and be like I need to change this like Where's the code for this page kind of thing? So it'd be nice if we could get a list of all the routes in the system Well, you're gonna go back to that bin console And this is actually similar people that are using a Drupal console Drupal console has a lot of the same commands as Symphony's bin console. They're kind of based off of each other So if you're on bin console debug colon router, you're gonna get a list of every single route in the system All these routes up here on top are things that help along that web debug toolbar at the bottom They're not there in production by the way, but that's what those are But here's our guy right down here hello under score world machine name and there's our URL So this is a really really good way to figure out like what the heck is going on What did I just inherit that I need to figure out how things are working? Also, let's go back to this very hipster cool looking guy here If you hover over these then you get more information And this is probably the most useful one because this is actually telling you what controller was called and Also, what route was matched? That's the machine name I this has got a slightly different machine name then because I'm mixing up some screenshots here Also, there's a ton of other information. This is actually I'm covering it here 87 milliseconds is the response time This is actually telling me who's authenticated. This is another thing that tells me how what templates rendered and things like that Now this is cool But if you click any of these links like click any of these things down here you go into something else called the Profiler so basically this explodes into about a hundred times more information And you have all kinds of different sections along the left here So doctrine is the library and symphony that people usually use to make database queries So I'm not making any queries on this page But if I were I could click the doctrine thing here and it would show me every single query that was being made in my system You can even run and explain right there on those queries and figure out, you know get do the mysql explain security events Events whoa, we're gonna talk about events logs Let's see here forms exception all kinds of stuff the most important one probably the most interesting one Oh, actually, this is kind of interesting that twig one actually shows you like all the twig templates that went into building your page And how long they took to render so that's kind of cool the most important one is the performance tab and it looks Well, I can only fit part of it on the screen like this This is actually meant for profiling your application because it shows how long every little bit of PNPs Took along the way between the very beginning of the request to the end of the response. However It's even more interesting as just a debugging thing like figuring out like what the heck is going on What's the magic that I can't see because the only thing that we do as developers is we make a route and we make a Controller, but there's something that happens before and after that route is matched There's something that happens before and after that controller is called and I don't know what those things are This shows you what those things are and by and large these are listeners Which you can think of as basically things that are implementing hooks And we're gonna talk about those in a second, but this is gonna be a way We can actually see like what's all the hidden magic behind the scenes so you can see a bunch of stuff here And I ran out of space here But actually about right here. You'd actually see a little bar that said Controller that would actually be where our controller is called and then there would be a whole bunch more stuff down here All right good. So can we do even less work? So I lied to you earlier It's not a surprise because I told you I was lying to you about the yaml thing in symphony Typically in symphony we do our routing with annotations Which is not done in Drupal. However annotations are something I'm sure you guys have all seen annotations already worked with annotations Annotations are something that you see in Drupal 8 quite a bit just not exactly for routing So now when you create a page in symphony with annotation routes you only touch this one file So hey, I want to create a new page. I would say great Go to can go to a controller class create one if you need one or reuse an existing one And just create one function and put the annotations above it the route and the controller are right in the same spot These are obviously PHP comments, but it's just a way of configuration So instead of having my configuration in the yaml file I've moved it over to my class and I have had people that absolutely love that I've had people that have told me to get out when I told them that annotations are a very very polarizing thing. I like it because Because of the context of having these things right next to each other So you don't have to like wonder what what's the URL to this function because it's it's right there But ultimately you have that Router debug the debug router been console thing. So you've got lots of ways to figure out like what? What the URL is to a controller anyways? All right, so the first half of this whole thing is all about routes and Controllers so cool. So we got that so 50% of the new stuff is it's done We got it. We're putting that away. So the whole second half of symphony and Well symphony and all the new stuff that comes into symphony or Drupal from symphony is all about services and The container the container dramatic done done done. All right. So first services are useful objects Okay, so that's another like overloaded word like controller. It's like hey, you should turn that into a service And you're like, yeah, totally you're like googling. What is a service? And they're like a service a service oriented economy is one where people work jobs and you're like damn It's damn it. It's such a generic word Services are useful objects. I know I gave a workshop yesterday and I realized when I said that That it sounds a little weird because you're like wait, what are an unuseful objects then what's not a service? So you can totally mess this up But in a fairly classic object oriented model, you have two types of classes You have some classes that just basically do work so you can think of like a Object that does translation it just does work you call a method you give it like some key It gives you back like the Spanish translation. It does work those types of classes don't typically hold data They don't typically hold a lot of state if you want to use that somewhat trendy word in programming So you don't like set properties on it usually have a whole data for you It just does work for you the second half the other half of objects are ones that don't do work They just hold data. They're just kind of like dumb model objects. Sometimes they're called data transfer objects in other languages So yeah, so usually they have like you they're holding data I think I don't think I gave yesterday that I wasn't sure if it was right or not because like I don't know that much about Actually using Drupal day-to-day, but nobody like shouted at me There's a couple people that read my training right now. This is probably wrong And they're probably like God. No, we were nice, but don't don't say that again I was thinking like the node object is something that primarily holds data But doesn't do a lot of work accurate Yeah, now you see you can start to cross the line here because you can actually take a nice little model object something that just says by the way The model objects can do basic work The classic example I gave is if you have a user class and it has a first name and a last name property You can put a function on there called get full name That has a you know first name last name with a space in the middle So it will do some work But it's all just kind of like minor work related to the data of that object now You can take that user class you can start tacking on other methods on it like send emails to all users And that's you actually starting to mix these two ideas so if You know when you get into more object oriented architecting thing if you just remember the rule of make a class either do work and Not hold data or make a class hold data, but do not do work You're probably going to accidentally end up with a pretty nice architecture So just kind of keep that in mind. So anyways the services are the objects that do work for you They're your workhorses now in symphony and Drupal Pretty much everything is done by services pretty much all the work if something's being done It's probably me being done by a service And so there's a lot of these service objects floating around there's a service object for like logging and for translating and for twig So we have to kind of keep track of these so this is where the idea of a container comes in Which I know is another like major buzzword when you start getting into this stuff So container is basically just an associative array of all those service objects because they're all flying around out there So let's see if we can hone them in a little bit and actually put them in one spot So we can kind of keep track of them now It's an associative array so that means that every one of those has a nickname or a machine name so if you want to get out to the If you want to get the the logger object you might read some documentation and say the nickname or machine name for that is Logger or is logger dot factory which I think is the one that it is in Drupal So you just got to figure out like what the nickname is to the object you want and you can just go and get it So cool now that so this is another lie small lie It's not actually the container itself isn't actually an object So this is actually the more accurate slide it is in fact or it's not an array It is an object, but the only method on it is get the only method that we care about at least Get you give it the machine name it gives you the object back So think of it like in an associative array of service objects Great. So as I mentioned There's a lot of services in Drupal and symphony that are put into this container out of the box These are tools a service is a tool. This is a good thing and If I give you guys the container Then you're dangerous because you just got it you can just grab stuff out of it You can do anything inside of symphony or Drupal because you have everything at your disposal So the question is actually here we go The question I'm gonna ask in a second So actually in symphony the container is actually loaded with 220 built-in objects And symphony doesn't give you a lot of stuff out of the box We ran this yesterday in my training and it was 560 or something like that for Drupal. So there's a ton of useful objects that are sitting in that container By the way, if you run debug container without piping into word count dash out You're actually going to see a giant list of every single service the container That's a very very powerful idea of the service container because in more in Drupal 7 it was more about just like knowing function names Like if I want to get something done I just call function name and that was that was cool And that was easy But there's some real power here to basically being able to list out every single tool you have There's no way to kind of list out all of the useful functions you had before So this is really really powerful and when you install new modules, they will give you more services So when you install a contrib module, you will all of a sudden have five or ten new services in this list new tools at your disposal And if you want to you can actually grab one of the those you can run that same command and actually pass in a specific machine name Like twig and it's going to give you more information about it very critically It's going to give you the class name so in lieu of documentation You could actually look through the container for a service that you needed and then actually look at its class name And just kind of figure out what methods you want to call on it just by knowing its class name all right, so Now we really want the container right because like cool I'm really dangerous if I can get the container. How do we get the container inside of a controller? This is slightly different in jupylate. This is probably the slide That's the most different between symphony and jupylate the way you get access to the container is a little bit different in jupylate in your controller But it's just a small thing. It's not like a big philosophical difference Just kind of a word of warning so in symphony. We just give it to you. We're like here. Yeah, boom You just get it in your controller on a container property boom present early Christmas present So you're gonna say this error container or get fetch out the service you want and then just start using it How do we know that twig has a render method? Well either you're fancy and you look at that class that is associated with that service Or you're probably reading some documentation because you're reading about how I how do I render templates? You know the most important thing is when they tell you when you google for how do I render templates? And they tell you to get the twig service you guys need to understand the whole idea of a service container I'm just getting out a useful object. It's machine name is twig. That's what the most important thing to understand And here I'm running a twig template in symphony and this is actually what it looks like if you're curious So twig is pretty much the same between twig and Drupal except we're in symphony We're actually responsible for building our entire page So we'll actually extend a base layout where we actually define the HTML whereas that's done with themes in Drupal So here's our same model here, but I'm gonna tack on an extra thing There's a container full of useful objects and it's accessible in the controller And we can use that to get our work done because remember the only rule on a controller is you need to return a response object But now you have this big bag of objects to help you they're optional objects So nobody's forcing you ever to use something out of the container you guys can get your work done in the controller However, you want to if you want to make a curl request out to a big Java app and just get the HTML that way and return it And response object that's cool. That's up to you so you can use those objects or you can not use those objects like sometimes People especially the symphony will forget that like they like try to force everything through a tool When sometimes it's just a really simple problem. They could have just solved it easily themselves So what else does symphony do? That's a bad question symphony doesn't do anything symphonies boring snooze Symphony doesn't do anything the services in symphony's container do things There's literally nothing that symphony does that is not actually done by one of it It's the services inside of its container So just a couple of quick examples if you're using a database in symphony You're probably using a library called doctrine and that's something that we actually kind of give you a service in the container to Queer the database so the code here is not important except for that first step Which is I go out and grab a service of a certain machine name and then I can start using it If you want to use like the PDO directly or there's actually this Library around it called D ball doctrine D ball But anyways, there's a service and you can just go out and fetch that service and start making queries So we're just using our tools So also a form systems simply has this big form components. Guess what the form components which is actually quite complex in symphony All boils down to a single service So in reality if you wanted to like use the form system and do all kinds of fancy things form dot factory That's what you go through always comes back to services So anyways, this is not really important for this But this is how you build a form in symphony and that's how you render a form in symphony different than jupylight jupylight's form system is different Or you can just by the way as far as forms go you can just do it yourself So one thing to remember also is like there is a request and a response object in symphony in Drupal It's the same object and if you want to you can always just go grab symphonies request object and just grab Information off of it headers query parameters post parameters. This is actually kind of funny request error request Who's whose idiot idea was that right? Technically I had to look this up because I was like this is so silly There's got to be a reason behind this technically in the HTTP spec post parameters are called request parameters So this is like one of those times where like this is correct even though it's weird for us to look at so there you go So later like six months from now when you need to get post parameters, and you're like what the heck You can remember, you know, this is correct even though it's weird And there's tons tons more community bundles out there just like in trip modules and again when you install it the number one reason That you install a contrib module is because it gives you more services Okay, they also can do other stuff in your system. That's the same thing with bundles and symphony I install a bundle by and large because it adds more tools to my container It might also add more routes and controllers It might also add some translations and things like that But the fundamental thing is it adds more services to my container and if there is one container got that question yesterday great There's they one container for your entire application. It's not like every module has their own container or something like that so We're using things out of the service container. We also need to well We also are going to want to eventually create our own services and put those in container and this is critically Important so here's here's our setup here We are now going to select a random greeting Okay, and we're going to pretend like this is a big long ugly chunk of code like a hundred lines of code and and we're trying To decide if we want to just hack this in our controller or if there's a better way to organize our code So we say you know what I don't want to hack this inside of my controller Because we actually maybe want to reuse this somewhere else. Maybe you want to unit test it You know we just you just know that kind of moving it into its own function is a better idea So back in the olden days. We may have just created a new flat function and thrown the 50 lines of codes in there Now it's kind of a theme right? I'll say we may be used to just use a flat function now. We're going to put a method inside of a class That's going to happen a lot when you're kind of transitioning. We're still going to isolate this hundred lines of ugly code to a function It's just going to live inside of a class so here is our Here's our class here. It's our random greeter and selects from one of these here and We call Public function randomly great give it the name and it hooks us up with a nice cute message. All right awesome So this has nothing to do with symphony or Drupal. This is justice man Like hey, this is a pretty cool class. We just made here and we're all feeling good So all right, so how do we use this in our controller? Well this step so far has nothing to do with symphony or Drupal either It's just object-oriented coding notice. I did not make this static There's a good reason for that so far. I could have made this a static function But I didn't and you won't make very many static things inside of Drupal or symphony So since that's not a static method. It means I need an instance of that object. Okay, so that's no problem I'm just going to in my controller instantiate a new random greeter and Then I'm gonna call the method randomly greet on that and it gives me back my greeting and I pass that into a template So cool. So this is just just object-oriented code organization kind of stuff Great so Could we log something we log in our controller which greeting was used? Okay? Well first thing is you're gonna be asking is the system. I'm using symphony or Drupal Do they already have a logging functionality? Well, both of them do so then it's a matter of what is the service? machine name that I need for that and in this case I can actually Well, you can actually guess it a lot of times But you'd probably actually Google how do I log in symphony and they'd say it's the logger service So this is the logger service. It's telling us what the class name is It's awesome. So, um, remember we're we have access to the container inside of our controller So we can just say this arrow container arrow get logger and then we just call a method on it Which again you'd look at the documentation or open up that class and say what methods are inside of this great There's a method called info and that's it So when you're working the controller, this is how you're gonna do most things You're gonna just say I'm just gonna go out and get that service and start using it and feel really good about myself So this is where the curveball comes in Could be logged from inside of our random greeter inside of our controller So the most logical step is we're gonna take this code here We're gonna copy that paste it in the other guy and see what breaks or it doesn't break, but it'll break So here we go. Yeah, awesome. We're feeling good. We run that Probably half of you are already thinking why this isn't gonna work There is no property called container in this class. This class does not extend anything Drupal doesn't really care about this class. This is not a class that symphony cares about This is just a class we created. There's no container property. So that ain't gonna work the whole thing where you get a Container property magically only applies to the controller the controllers It's like special first entry point of your application So symphony gives you the entire container because it's like you're probably gonna need this body good luck out there Okay, so just kind of passes that to you, but it doesn't happen anywhere else So, oh my god dependency injection. So this is a term that's like you guys are hearing a lot have probably been hearing for a long time There was a presentation early on earlier on dependency injection It's the easiest idea ever with the hardest name ever and also it's like the most confusing easy idea ever Can't decide if it's easier hard basically, but we're gonna show a quick example of this So the scenario is this I'm in I am inside a service. By the way, this is a service We have not put this in the container yet, but this is a service It's a class that does work for us So whenever you find yourself in a service and you don't have access to something that you want You're like, I need the logger object or I need an API key doesn't have to be an object Sometimes it could be configuration like I don't have access to something I want in olden times This would have been where you'd use the global keyword. You're like screw it I'm a global database connection and then we're good to go or it maybe call a static method static When you call stack method does actually the same thing It's actually a way to just kind of reach out of the global scope. We don't do that anymore There actually is a couple ways to still cheat in Drupal. I'm not going to show them to you, but you can find them But basically we don't have access to anything inside of our function In fact, the only thing we have access to is a name variable So what we're gonna do is we're gonna force somebody else to pass it to us That's what dependency injection is it says look if I need something I need the logger So I'm gonna create a construct function I'm gonna put logger as an argument and I'm gonna put it up on a logger property now This is not fix our problem It just basically screws over whoever is instantiating us because now it's their problem to pass us the logger objects It's not like this magically works like something like magically like found this and like put it in there Our code is broken at this point But oh and if you want to be super trendy you can figure out the class name or interface behind that and type Hint type inning is optional if you're new to this stuff Don't worry about type inning if you're getting a hang of this stuff type inning is totally rad It gives you better auto completion and better PHP errors if you mess stuff up, but it's optional All right, so and then down here because we know that we've screwed over the other guy who's got to instantiate us We know once if they're able to somehow pass us the logger we have logger on a property So down here we're able to actually use that logger Okay, so this class is now perfect, but our application is still broken So we need to go back into our controller But hey in our controller we have access to the logger so hey problem solved when we instantiate the random greeter We'll just pass the logger in as the first constructor argument and that is dependency injection Cool when we needed a second service a second object instead of a random greeter It would just be a second constructor argument in a second property. Okay, cool Okay, the last thing we're gonna do here is Checking my time because I want to get through everything. Well, I'm always through promise I won't keep you guys forever, and I'm not over time yet. I'm just aware of my time Last thing we're gonna do is actually teach symphony teach symphonies container specifically how to instantiate our services So it's kind of silly as it seems We are going to literally centralize That three lines of code or really one line of code instead of creating the random greeter We're gonna send put that in a central spot So we can just ask that central spot for the random greeter object and it will instantiate it for us There's several advantages to doing this one of them is that if you have this code in like ten different places like ten different controllers And then tomorrow you decide to add a second constructor argument to random greeter. You got to go update all those ten places Another thing is that if we put this in the container a property of things in the container is when you ask the container for a service You say how I want the logger service if you ask for it ten times It always gives you back the same one instance Which usually what is what you want because you don't need ten loggers to log ten messages You just need one logger and you call info on it ten times So it's kind of a nice way to make sure you don't have extra objects flying around that you didn't need because that's Just extra memory you didn't need Cool so the way that we teach it is via yaml file and it's very simple because to instantiate an object There's only two pieces of information you need to know what's its class name and what are its constructor arguments So there's a services.yml file in symphony. There's also a services.yml file inside of your Drupal module It's actually module name dot services.yml But it looks exactly like this. This is copy and pasteable into your module You'll have a services key. You'll create a machine name Which is whatever you want because clearly that's a stupid machine name so that that could be anything and then it's just class It's the full name to main space your class and then it's an arguments key Now this is actually and I know realize the yaml is still somewhat new to you guys But what this is actually creating is an indexed array So this is the first constructor argument if I had a second constructor argument It would just be another dash down here and another dash down here and another dash down here There's also a single line version of this you'll see which is identical Which is square brackets and then it'll be like at logger comma the second comma the third So sometimes in yaml you can do things in a single line or you can break it into multiple lines So they're not doing different things. You'll just see both ways Now the key here is if I left off the at here, then it would actually pass the string logger So they had to be some way when we created symphony for like us to kind of indicate We don't want the string logger. We want the service logger So we just kind of invented the at symbol so the at symbol is a total kind of special symphony containerism It's just used in this spot and that indicates we need the logger service created So now we can ask the container for the my random greeter service and it's going to instantiate it for us And that's very important. We're gonna ask for the my random greeter. It is still going to instantiate it There's still some code that's going to say new random greeter open parentheses and then pass in the logger object That didn't go away. We just centralized it to this kind of invisible layer so what that means is We can actually run debug container and we're gonna see our service inside of there So we just went up from like 220 to 221 services inside of our container We added another tool to our container and now we can use it inside of our controller Just like this go out and get it. We don't have to instantiate it manually anymore All right, cool. It's the last thing real quick This is very very important especially when you start diving into the magic of Symphony and Drupal so the first the first two parts the routes and controllers and the containers Those are the two big guys. This is like kind of the third big ish guy, but less big I guess so Drupal has hooks symphony has events and you'll find just like hook menu They do the exact same thing they just have different terminology the biggest difference between hooks and events are that hooks You had to name something a specific way and then it was magically called with events You don't have to name your function anyway You can just name it whatever you want, but there's an extra step that says when this event happens Call this method. So there's a little bit of configuration that does that So this is back to our same model when symphony or Drupal is going through its request response cycle There's different events that are it said the word we use is dispatched There's a different events that are dispatched. These are just these are the same as like if you were using like module handler and actually What's the word basically firing a specific hook and it would call all the The functions for that hook. It's the same idea here. So symphony has an event called kernel dot request That's just what the event is called in the same way that there are hooks that just have names And you need to kind of Google to figure out what those names are There's one called kernel that request that happens during this part of the process Another event that happens here and another event that happens here So when I say event just think like that's a hook And if I want you hook in at that point in the request response cycle Then I'm going to add a listener on this specific thing and the listener is just a function So let's do something crazy. So what if we didn't return a response from the controller Which is like done done done because so far I've been saying like you always return a response from the controller You have to return a response from the controller. That's not technically true and actually in Drupal You rarely return a response from the controller. You typically return a render array So we can actually very easily write that same thing in a symphony. I'll actually show you how that works in Drupal So now I've updated my controller to return an array. I'm just inventing this array This is not significant in any way. I'm like, I don't know let's create a template key and a variables key Let's make something else actually render the template somewhere else. So if you do that, you're going to get this terrible error The controller must return a response. That's what I was trying to tell you guys the controller must return a response Okay, what are you doing? I'm so angry right now. Okay, or you'll get this if you have the bundle installed So okay, so what you're actually going to do is you actually hook into one of those events There's one event that happens after the controller if you don't return a response It's actually called kernel dot view and an event listener looks like this again It's actually a class it's a class that implements event subscriber interface I realize that's a little hard to read and you just need to have two methods on it This one down here, which basically says when kernel dot view happens call this method on view Which could have been called anything So this is going to be called and obviously I'm already kind of planning ahead. I know I'm going to need the twig service So I'm already kind of planning for dependency injection here So this is cool. This is looking good already. Step two is you're going to register. This is a service Boom looks exactly the same way. You still have your other service up here now There has to be some way for us to be like no, this is not a normal service. This is an event subscriber We need to some way kind of like notify a symphony or Drupal to know that this is an event subscriber Just us having that class there and having it registered as a service is not enough for it to be auto discovered So we have to raise our hand like no no no my service is special and you do that With a tag This is a little bit weird looking, but you're literally it's like tags isn't like tagging a blog post You can just add tags to your service. You're like this service is tagged with delicious It's the pizza service like you can just make up tags if you want to but when the container finishes being built There's actually a process that runs that says please give me all services tagged with Kernel dot event subscriber and it gets those services and it kind of notifies symphonies core that those are event subscribers So there's a finite number of tags in the system But typically if you're hooking into some core part of the system, you're going to use a tag You're gonna create a service. You're gonna Google. How do I do X? How do I hook into X process? They're gonna tell you create a class make it implement some interface or extend some base class And that'll be different depending on what you're doing Registrars a service and then tag it with Something something that kind of hooks you into that system. So you're gonna see that process quite commonly inside of Drupal And this is less important, but this is actually what our function might look like This is actually goes and gets that array that was returned from the controller and actually passes it to our twig render And then ultimately gets the HTML that came back and puts it into response object That's actually how the render array works in Drupal Obviously, it's much more complicated than that the implementation of it But it's a listener on kernel that view and and it does different things kind of reads the format you want and and does various things all right, cool, so Be nice if that's what I always think it'd be nice if I could like see like a fully featured symphony app I could poke and see how it's doing all right rad So remember that symphony installer if you just instead of saying symphony new say symphony demo It's gonna download the symphony demo project, which is something that we maintain We try to pack lots of features into it and it's just a fully feature kind of best practice See nice symphony app that you guys can hack around with and try to break and unbreak and break and unbreak So I highly recommend that And here's what it looks like it's even styled somewhat nicely because I didn't style it Actually, I styled the first version of this and somebody came through and fixed it God bless designers Also when you're using symphony or Drupal, I highly recommend using Pete's we stormed There's a lot of auto completion like a lot of long you statements and things like that You're gonna if you're not super opinionated if you're willing to try it You're gonna have a good time if you use Pete's we storm there's also an amazing symphony plug-in which gives you all kinds of crazy auto completion in symphony and Drupal that we have No business having and so I highly recommend installing that if you go to that bitly right there We have a free tutorial on Pete's we storm and to like walk you through installing that and lots of other shortcuts Also came to university that's some bias. It's the best way to learn symphony. So come check us out I'm gonna have a coupon code in a second you guys can look at and also use silks because it's all the same stuff So if you want to kind of get into this stuff slowly use silks and you're gonna be good to go and use Drupal 8 Drupal 8 can Be used to learn symphony or a silks. So look it's a big happy family. We should have put like a big heart around it you know so Again the fact that I can give this presentation is a testament to me being a symphony guy and I'm actually pretty darn good at Drupal and As you guys get into Drupal you're gonna find that you're pretty darn good at symphony on accident, which is pretty cool So principal themes aka I don't remember what the presentation was about But I keep having these words in my mind when I try to go to bed route controller response services the container and event listeners those are the three critical things and Drupal 8 and symphony share the following things Request and response objects routes controllers response event listener system services and container system That's all exactly the same you can get a list of all the services with Drupal console You can get a list of all the routes with Drupal console. Ah Man, there's no web debug tool bar in Drupal kidding. There is yay It's in the develop module. It's a sub module and develop called web profiler and and it's pretty pretty rad It's been around for a while and it's awesome. It actually has like ten times more stuff than symphonies does So there you go. So you can use silks to learn Drupal. You can use silks to learn symphony You can use symphony learn Drupal. You can use symphony to make you a sandwich And finally we have more tools to solve more problems. Thank you very much This by the way is a coupon code for a free month on our site. So go grab that we have a Tutorial similar to this we have lots of symphony stuff. We're gonna leave that up for like a very short amount of time So, you know create an account and register and use that There you go. So thank you guys very much