Introduction to the Symfony 2.7 Series
Hi this is Chris from Codereviewvideos.com and this series is all about a beginner's introduction to Symphony 2.7. So it's a beginner's introduction to Symphony rather than to PHP, so there is an element of expectation of PHP knowledge. It's only a basic expectation, but you do really need to have some understanding of PHP. In this video, we're going to take an introductory look at Symphony, specifically how and why you would probably want to use it. We're not going to get too technical in this video, but we are going to look at how the code is structured and we're also going to look at the Symfony demo application.
In future videos in this series, we're going to cover routing and the command line. We're going to cover Doctrine, which is effectively our database. We're going to cover forms, validations, security, and then testing. Now if you're anything like me, the very first time that you took a look at Symphony or Laravel or Zend framework, you probably felt quite overwhelmed. There seems to be a lot of moving parts and some of them have very strange names like Doctrine and Twig.
Now, the good news is you don't need to understand how these components work before you can get started. It does help, of course, if you've read the documentation, but truthfully, sometimes it's easier to see the components in action and then you get an understanding of how they're used. And then you can go and read the documentation and it gives it a bit more context, at least that's how I find I work best.
Core Concepts: Structure and Request/Response
There are two key things to understand about using a framework like Symphony. The first is that it's designed in such a way to be a set structure to any any project that uses Symphony. So if you go into a brand new project, but it's already using Symphony 2, then the good news is you'll feel right at home because no matter which app you're working on, it always follows a similar structure. So the routes will always be in the same place, the controllers will always be in the same place, the database entities will be set up in the same way. And that's nice because it allows any developer, whether it's you or a new team member, to immediately feel comfortable working on the project.
The second thing is that Symphony is really all about request and response. So request is every time that we hit a URL. Behind the scenes the server is doing something. So if we open up the developer tools and then we just refresh this page, so just return on that, we can see that in the background there, behind the scenes, there's all all these things happening. And you're probably aware of like response codes like 200 response or 404, etc. And really that's like the response, but our request could be something... so if we go to say the documentation page for example, like this part of the URL is quite important as part of the request. And behind the scenes, Symphony is going to handle that for us. Now we'll set all that up and we'll see it all in action. But you can see behind the scenes of any web request or any web process, there's the response, as you can see, and then the request. So the request is the thing we ask for, as it sounds like, you know, we request something, and then response is what in our case Symphony's controllers and routing and so on will generate for us. And that's the the gist of Symphony in a nutshell.
Installing the Symfony Demo Application
So let's go ahead and get started using Symphony 2. So we go to the documentation and then we go to read the book and installing and configuring Symphony. So at the very top here is how to install the symphony installer on a Linux or Mac system. I'm on a Mac so I'm just going to go ahead and copy this in exactly. And what this is doing is just downloading the installer file and then changing the permissions on it so that it is executable.
The next thing that we want to do is go down to installing the Symfony demo application, and you can see all we need to do is just type in symphony space demo and that's going to go ahead and download the demo application for us, which is good. And then once it's done, it's going to give us a bit of an error message here about not having the date time zone set in our php.ini. Now depending on how you're set up, depending on your system, the location of this file may be different. So Googling is best honestly in this situation.
Configuring PHP's date.timezone
But on a Mac, it's going to live in the /etc folder. So what we're going to need to do is just copy the default one, php.ini.default, just hitting tab there to autocomplete. And then we want to put it into php.ini and I need to do sudo there as well because it's a protected directory. Then we also need to do sudo vi /etc/php.ini. If you prefer a different editor, by all means go ahead and do it, but yeah vi is available pretty much as standard on any Linux or Unix-based system. So I'm just going to edit that.
And then as soon as we're in here, now using vi is a little bit tricky, so I'm just going to tell you exactly what I'm typing: forward slash, you can see at the bottom there, and then I'm just going to type in timezone. So that's going to jump me down straight away to where I need to put this in. I'm just going to scroll across using the arrow keys, press X over the thing that I'm wanting to delete, getting a warning there but don't worry about that. And then going back onto the equals, pressing I for insert, and then right arrow, and then I just want to put in here Europe/London like that. As soon as you finished editing, press the Escape key and then colon WQ for write and quit. I think I probably need the exclamation mark. If you've made a mistake here, just put Q and then exclamation mark and that'll just exit you out, no changes saved. But write and quit, probably need the exclamation mark to force it. Okay, so we're all good. And then now what it's saying is if we rerun this then we should be all good. So let's just do that and as you can see there, we're all good to start running our demo application.
Running and Exploring the Demo App
Now if we scroll a little bit further back up, we can see that we need to run this php app/console server:run command. And then that's going to allow us to start our web server. I'm not even in the right directory there, that's no good. So change directory into symphony-demo, paste that command back in, and you can see now that we've got our server running on this URL. So let's open that up and we can see the Symfony demo application is running for us.
Now what's really nice about the demo application is this was brand new in 2.7, Symphony 2.7, and if we can go into the application and look around, and also we can show the source code. So we can look at what's happening and we can see like the path to where the file is, the actual code that's running this file, like the template code and we can sort of use this to get an idea of how a Symfony application is structured. So if we go back to the demo application, we can also then go back into the backend. And we can see here like there's a sign-in thing set up for us. And again, you can see all the source code and we can see really like behind the scenes how an application is sort of structured. So that's cool. And again you can see everything in use here, like we can see that we're getting Doctrine, so like getting access to our database, pulling stuff out of the posts table effectively, and so on.
Setting Up the Project in PHPStorm
But what's difficult about this, really, is that whilst it's fine to see it like that, it makes a bit more sense to see it in context. So if we jump back across and we're going to open up PHPStorm. PHPStorm is an IDE for doing PHP development, like an Integrated Development Environment, that's what IDE stands for. If you've never used PHPStorm before, it will genuinely make your life a lot, lot easier than just doing it using Sublime or God forbid, Notepad or something like that. I highly recommend that you use PHPStorm. There's plugins available for Symphony which makes development even easier.
So we're going to do create new project from existing files. We're going to say source files are in a local directory and there's no web server configured. That's just because we're running our web server locally. We're going to go to Users, Code Review, and then... development should be in here if we give it a refresh... development, symphony-demo. Okay.
Configuring PHPStorm Project Root
So what we need to do is this is the top level folder that we installed in. So if we go back to the command line, in here, let's just cancel out of that, do print working directory. We can see we're in users/codereview/development/symphony-demo. That's our top level directory. So what we need to do is just tell PHPStorm that that is our project root. Just going to tick project route there, click finish, and let it do its thing. And this is going to go off and sort of index all the files and everything. This allows us to have like autocomplete, smart autocomplete as we're typing stuff in. Very, very cool.
Overview of the Symfony Directory Structure
Anyway, what I really want to cover in this is like the standard Symfony directory structure. So this is what you're going to get as soon as you install any Symphony project using the installer or if you're doing it the older ways like using Composer or whatever, this is the structure that you will end up with. And this is the structure that you need to sort of get familiar with when using Symphony.
There's not that much to see as such, but like there's four directories that really make a difference. The bin directory we can for now just completely forget about. The app directory is where like all the configurations are going to live. So like our config files all live in here. Our settings files will be in here when it opens up, and we can go through and figure all this stuff out in in future videos. You can see routing, security, and so on. So these are like the settings files. Again, app/config is quite a common directory structure that we're going to go into. resources is quite a common directory structure and of course even when things go wrong, we will want to look into our logs directory.
Our src directory is where we store all our custom code. So all the code that we're interested in that we're writing, that's where that's all going to live. And again these folder names are a little bit confusing when you first starting out but we're going to cover each one.
And then you got the vendor directory. And what the vendor directory is is all the third party code that behind the scenes makes Symphony run as a framework. So we can add stuff to this but effectively, if you've ever heard of like packagist.org, I think it is, let's just jump across there, packagist.org, then this is where we can go and get any dependency. So like third party pieces of code and we can pop that into our composer file and I'll use Composer just automatically built in out of the box and it will download third party code and then we can just sort of plug that in. It makes developing much easier because effectively we're just reusing other people's well-tested and battle-hardened libraries to make our development lives much easier. So that's really the the gist of what the vendor directory does.
And then lastly there's the web directory. Now you won't do a huge amount of stuff in here. The main one really is the app_dev file that when we go into our development environment... sometimes you'll get like it says you're not allowed to view this file and what not. app_dev is where we do all our development. That's what gives us the toolbar down at the bottom. So if we go back to our demo application, that's where you see the the toolbar in app_dev but you won't see it in the like the production environment, or you shouldn't, as long as it's all set up okay. And really that's about it for what we need to know about at this stage. Behind the scenes, all this stuff will be generated for us, but we'll cover that again. But yeah, that's the gist of a Symfony directory structure. You'll get used to seeing it more and more, and sooner or later you you'll come to a point where actually seeing that directory structure is quite reassuring and it makes development much easier.