Archived video

This video is archived and won't be updated. We're keeping it online for now as a courtesy.

Alternate resources: 

Download and Configure Symfony

Video loading...

  • 0:00
    Starting in Symfony2
  • 0:01
    Download and Configure Symfony
  • 0:04
    With Leanna Pelham
  • 0:08
    OK let's get Symfony downloaded and set up.
  • 0:11
    Head over to symfony.com and click Download Now.
  • 0:15
    If the site looks a little different for you,
  • 0:17
    that's because the internet loves to change things the minute
  • 0:20
    we record a screencast.
  • 0:22
    But no worries, just find the download page.
  • 0:24
    All the steps will be the same.
  • 0:27
    We're going to use a tool called Composer
  • 0:28
    to get the project started.
  • 0:30
    Composer is PHP's package manager.
  • 0:33
    That's a way of saying that it downloads
  • 0:35
    external libraries into our project.
  • 0:38
    Oh yeah, and it's also the most important PHP innovation in years.
  • 0:43
    To get it, go to-- you guessed it-- getcomposer.org and click Download.
  • 0:49
    Depending on whether you have cURL installed, copy one of the two
  • 0:52
    install lines.
  • 0:54
    Now, open up your terminal and start typing wildly.
  • 0:57
    Hmm.
  • 0:58
    OK, that didn't work.
  • 0:59
    So let's try pasting the command instead.
  • 1:02
    If you have PHP 5.4 installed, run this anywhere.
  • 1:06
    We'll use PHP's built-in web server.
  • 1:09
    If you don't, get with it.
  • 1:11
    PHP 5.3 is ancient.
  • 1:14
    But anyways, make sure you have Apache set up
  • 1:16
    and run the command at the server's document root.
  • 1:19
    This downloads an executable PHP file called composer.phar.
  • 1:24
    If Composer complains with any warnings or errors,
  • 1:27
    follow its recommendations to make sure your system
  • 1:29
    doesn't panic when we use it.
  • 1:34
    Go back to the symfony.com download page
  • 1:36
    and copy the create-project Composer command.
  • 1:44
    Change the target directory to say "starwarsevents"
  • 1:47
    and the last part to say "@stable."
  • 1:50
    This is the version number, and @stable is a neat way of making
  • 1:53
    sure we get the latest and greatest.
  • 1:56
    This tells Composer to download the Symfony Framework standard edition
  • 1:59
    package into the starwarsevents directory.
  • 2:02
    That's all you need to know for now.
  • 2:04
    We're going to explore composer later.
  • 2:08
    OK.
  • 2:09
    So downloading everything is going to take a few minutes.
  • 2:12
    Composer is busy with two things it wants.
  • 2:14
    First, it's downloading a little example
  • 2:17
    project that uses the Symfony libraries.
  • 2:20
    Even while Composer is doing its thing, we can open a new terminal
  • 2:23
    and move into the new directory.
  • 2:25
    It contains a relatively small number of files and directories.
  • 2:29
    Second, our project depends on a bunch of third party libraries
  • 2:32
    and Composer's downloading these into the vendor directory.
  • 2:36
    If you run ls vendor, you'll see that more
  • 2:39
    and more things are popping up here.
  • 2:41
    When that finishes, our terminal will become self-aware
  • 2:44
    and start asking us configuration questions.
  • 2:47
    Just hit Enter through all of these.
  • 2:49
    We can tweak the config later.
  • 2:52
    Let's make sure that our computer is ready to run Symfony.
  • 2:55
    The project has a little PHP file, web/config.php,
  • 3:00
    that checks our computer and tells us if we're superheroes on system
  • 3:03
    setup or if our machine is missing some libraries.
  • 3:07
    We need to navigate to this script in our browser,
  • 3:10
    so if you have PHP 5.4 or higher, just
  • 3:13
    use the built-in PHP web server.
  • 3:16
    Run this command from the root of our project.
  • 3:18
    If you get an error or are using Apache,
  • 3:20
    we have a note on this chapter's page about all that.
  • 3:24
    We now have a web server running at localhost:8000,
  • 3:27
    which uses the web directory as its doc root.
  • 3:30
    We can just surf directly to the config.php file.
  • 3:34
    We'll talk more about a proper web server setup later.
  • 3:37
    If you see any scary major problems, you'll need to fix those.
  • 3:41
    But feel free to ignore any minor problems for now.
  • 3:45
    You may see two major issues: permissions problems with the cache
  • 3:49
    and logs directories.
  • 3:51
    OK since this can be really annoying,
  • 3:53
    we've got to get it fixed.
  • 3:55
    Basically we need the cache and logs directories
  • 3:58
    to be writeable by our terminal user and our web server's user,
  • 4:02
    like www-data.
  • 4:05
    And if a cache file is created by one user,
  • 4:07
    that file needs to be modifiable by the other user.
  • 4:11
    It's an epic battle of two Unix users
  • 4:14
    needing to mess with the same set of files.
  • 4:17
    Of course, you're awesome and are using the PHP built-in web server.
  • 4:22
    For us, our terminal user is our PHP web server
  • 4:25
    user so we don't have any issues.
  • 4:29
    If you're using Apache or are having issues,
  • 4:32
    check out the sidebar on this page with some tips.
  • 4:35
    OK.
  • 4:36
    We're ready to get to work.
  • 4:37
    Check out our first real Symfony page by hitting
  • 4:40
    the app_ dev.php file in your browser.
  • 4:43
    Hopefully, a cute welcome page greets you.
  • 4:46
    The project came with a few demo pages,
  • 4:48
    and you're looking at the first one.
  • 4:51
    The code for this lives in the src/Acme/DemoBundle directory.
  • 4:57
    You can see the rest of the demo pages
  • 4:59
    by clicking the Run the Demo button.
  • 5:01
    Without writing any code, we already have a working project.
  • 5:05
    Yeah, I know it's kind of lame and boring now,
  • 5:08
    but it does have the normal directory structure.
  • 5:12
    Let's look at the app directory.
  • 5:14
    It holds configuration and a few other things
  • 5:17
    that tie the whole project together.
  • 5:19
    If your app were a computer, this would be the motherboard.
  • 5:22
    It doesn't really do anything, but it controls everything.
  • 5:27
    Most of our code will live somewhere else,
  • 5:29
    in directories called bundles.
  • 5:31
    These bundles are activated in the AppKernel class
  • 5:34
    and configured in the config.yml file inside app/config.
  • 5:39
    For example, there's a core bundle called Framework bundle.
  • 5:43
    It controls a lot of things, including
  • 5:45
    the session timeout length.
  • 5:47
    So if we needed to tweak this, we'd do
  • 5:49
    it under the framework config key.
  • 5:55
    Routes are the URLs of your app, and they also live in this directory,
  • 5:59
    in the routing.yml file.
  • 6:00
    We'll master routes in a few minutes.
  • 6:03
    You can ignore everything else in the app/config directory.
  • 6:06
    We'll talk about them more when we cover environments.
  • 6:10
    The app directory is also where the base
  • 6:12
    layout file and console script live.
  • 6:16
    More on those soon.
  • 6:19
    After app, we have bin.
  • 6:20
    You know what?
  • 6:21
    Just forget you ever saw this directory.
  • 6:23
    It has some executable files that Composer added,
  • 6:26
    but nothing we'll need at this point.
  • 6:29
    All the magic in code writing happens in the src directory.
  • 6:33
    We're going to fill it with subdirectories called "bundles."
  • 6:36
    The idea is that each bundle has the code
  • 6:38
    for a single feature or part of your app.
  • 6:41
    We're about 10 seconds away from nuking it,
  • 6:44
    but if you want to enjoy this demo code,
  • 6:46
    it lives here inside Acme/DemoBundle.
  • 6:49
    We already know about the vendor directory.
  • 6:51
    This is where Composer downloads outside libraries.
  • 6:54
    It's kind of that with a ton of files in it.
  • 6:57
    But no worries, you don't need to look in here unless you want to dig
  • 7:01
    around in some core files to see how things work.
  • 7:04
    Actually, I love doing that.
  • 7:06
    We'll tear open some core files later.
  • 7:09
    The last directory is web.
  • 7:11
    It's simple.
  • 7:12
    This is your document root so put your public stuff here,
  • 7:15
    like CSS and JS files.
  • 7:17
    There are also two PHP files here that actually execute Symfony.
  • 7:22
    One loads the app in the dev environment
  • 7:24
    and the other in the prod environment.
  • 7:27
    We'll go more into this environment stuff later.
  • 7:30
    It's time to get serious, so let's get
  • 7:32
    all of that demo code out of the way.
  • 7:35
    First, take your wrecking ball to the src/Acme directory.
  • 7:43
    Next, take out the reference to the bundle in your AppKernel
  • 7:46
    so Symfony doesn't look for when it's loading.
  • 7:50
    Finally, get rid of the Acme demo route import in the routing_dev.yml
  • 7:54
    file to fully disconnect the demo bundle.
  • 8:00
    Refresh your browser.
  • 8:02
    Yes, an error.
  • 8:04
    No, no, I'm serious.
  • 8:05
    This is good.
  • 8:07
    It's telling us that the page can't be found.
  • 8:09
    The demo page that was here a second ago is gone.
  • 8:13
    Congratulations on your completely fresh Symfony project.
  • 8:18
    Let's make our first commit.
  • 8:20
    We're going to use git but not much is
  • 8:22
    different if you use something else.
  • 8:24
    And if you don't use version control, shame.
  • 8:29
    If you already have a git directory, get rid of it.
  • 8:32
    Otherwise, you'll inherit the history from Symfony Standard
  • 8:34
    Distribution, which is about 1,000 commits.
  • 8:39
    Now don't go crazy with adding files.
  • 8:41
    There are some things that we don't want to commit.
  • 8:44
    Fortunately, Symfony gives us a solid git
  • 8:46
    ignore file to start with.
  • 8:48
    The bootstrap.php.cache file is generated when you run Composer.
  • 8:53
    It's super important, though you'll never need to look at it.
  • 8:57
    Regardless, since it's generated automatically,
  • 8:59
    we don't need to commit it.
  • 9:01
    The cache and logs directories also have generated contents,
  • 9:05
    so we should ignore those too.
  • 9:08
    The app/config/parameters.yml file holds all server-specific config,
  • 9:12
    like your database username and password.
  • 9:15
    By ignoring it, each developer can keep their own version of the file.
  • 9:19
    To make life easier, we do commit an example version
  • 9:22
    of the file called parameters.yml.dist.
  • 9:26
    That way a new dev can actually create their parameters.yml file
  • 9:29
    without guessing what it needs to look like.
  • 9:32
    Collaboration.
  • 9:34
    We also ignore the vendor directory because Composer
  • 9:37
    downloads everything in here for us.
  • 9:40
    If a new dev clones the code, they can just run php composer.phar
  • 9:44
    install, and bam, their vendor directory looks just like yours.
  • 9:49
    Everything is being ignored nicely so let's go crazy
  • 9:52
    and add everything to git and commit.
  • 9:56
    Find some friends.
  • 9:57
    It's time to celebrate the first commit to your awesome project.
  • 10:01
    Do some jumping high-fives.
  • 10:02
    Grab a pint and make a Chewbacca cry.
Loading ...

Download and Configure Symfony

Loading...

In this lesson, we're going to get things all set up using Composer. If you are not familiar with Composer, you can watch the Wonderful World of Composer tutorial to get up to speed. Once we have Symfony installed, we'll take a tour of the directory structure, and then we'll get things set up to start developing our application with Git.

Apache Notes

If you’re using Apache instead of PHP 5.4's built-in web server, and you've downloaded the project to your Apache document root, then you can go to http://localhost and find your way to the config.php script at http://localhost/starwarsevents/web/config.php. The URL for your app will be http://localhost/starwarsevents/web/app_dev.php.

Fixing Permissions Issues

The easiest permissions fix is to add a little umask function to the top of 2 files. Pop open your project in your favorite editor (we love PhpStorm). Open up app/console and web/app_dev.php. You’ll see a little umask line there. Uncomment this:

#!/usr/bin/env php
<!--?php
umask(0000);
// ...

What the heck? The umask function makes it so that cache and logs files are created as 777 (world writable). Once you’re done, set the permissions on the two cache and logs directories:


$ chmod -R 777 app/cache/* app/logs/*
You shouldn’t have any more issues, but if you do, just set the permissions again.

This method can be a security issue if you’re deploying to a shared server. Check out Symfony’s installation chapter for details on other ways to setup your permissions.
Downloads: 
Log in or sign up to download companion files.