Namespaces

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

Since we have this new movie form, we're going to want to save the data to the database. To do this let's create a Movie entity. I'm not going to generate it, let's just do this by creating a good ole fashioned PHP Class called Movie. But when I do this, notice the namespace box is empty. I could type AppBundle\Entity here, but I'm trying to do as little work as possible people!

Let's make the robots do this for you. Go into preferences, search for 'directories' and here you can see the three directories that we excluded earlier. Click on src and click the blue sources button above. Now we can see this on the right and clicking the little 'p' allows us to edit the properties. So, if we were using PSR-4 inside of this directory we could actually put our namespace prefix in this box - like KnpU. But all we needed to do was mark it as a source route, so we're done!

When we head back, oh look the Entity directory disappeared! Where did we misplace that to? Well, sometimes PHPStorm does that to empty directories. Temporarily I'll switch back to project view and now create a new PHP Class. Because of the sources change, it guesses the namespace part perfect. Call the class Movie:

<?php
namespace AppBundle\Entity;
class Movie
{
}

Okay switch back to Project Files and there it is. Excellent! In a second we'll set this up with some annotations, but right now I want to use it inside of my MovieController. We'll add a form here soon, and when we do, we'll need a new Movie object. So we'll say $movie = new... and I could end this with AppBundle\Entity\Movie -- but you really don't want to do that. Always go directly to the last part: in our case Movie, and let that autocomplete:

... lines 1 - 4
use AppBundle\Entity\Movie;
... lines 6 - 8
class MovieController extends Controller
{
... lines 11 - 13
public function newAction()
{
$movie = new Movie();
... lines 17 - 20
}
}

Because, when you do that, it adds the use statement on line 5 which is HUGE. If you are not using the autocomplete functionality for use statements, then you're doing it wrong.

Now, if something went wrong and you didn't autocomplete or you end up with Movie here but no use statement, you can import it if you want. Just hit alt + enter. Alt enter is one of the most important shortcut we are going to see. It's called the "actions" shortcut. Often when you're inside some code. you can alt + enter and get a list of actions to do. I'll select 'import class' from this list and it will add that use statement for us.

To take this a bit further let's add a public function listAction() that will list those movies from the database. I don't want to build this out now, I just want it to return a simple Response that says "todo". So let's add return new Response and select the Response we want, which is the one from HttpFoundation. Hit tab and the use statement politely puts itself on top of our file. Finish with 'TODO'. Now let's add a route above this, @Route("/movies", name="movies_list"):

... lines 1 - 7
use Symfony\Component\HttpFoundation\Response;
... line 9
class MovieController extends Controller
{
... lines 12 - 23
/**
* @Route("/movies", name="movies_list")
*/
public function listAction()
{
return new Response('TODO!');
}
}

Oh and now that we have this second endpoint, back in new.html.twig, if we want to create a link to this page, we can. And we can use another live template. Type a, hit tab, and use {{ path() }} and - amazingly - we even get auto-complete on the route name. For the link text, say "back to list':

... lines 1 - 5
<a href="{{ path('movies_list') }}">Back to list</a>
... lines 7 - 16

Refresh that, and there's our link right back to our list. Awesome!

So, don't. write. use statements. ever.

Leave a comment!

This is an older tutorial using an older version of PhpStorm. However, most of the tricks we show in PhpStorm still work beautifully.

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.3.9, <7.3.0",
        "symfony/symfony": "2.8.*", // v2.8.15
        "doctrine/orm": "^2.4.8", // v2.4.8
        "doctrine/dbal": "<2.5", // v2.4.5
        "doctrine/doctrine-bundle": "~1.4", // 1.6.4
        "symfony/assetic-bundle": "~2.3", // v2.8.1
        "symfony/swiftmailer-bundle": "~2.3,>=2.3.10", // v2.4.2
        "symfony/monolog-bundle": "^3.0.2", // v3.0.2
        "sensio/distribution-bundle": "~5.0", // v5.0.17
        "sensio/framework-extra-bundle": "^3.0.2", // v3.0.18
        "incenteev/composer-parameter-handler": "~2.0" // v2.1.2
    },
    "require-dev": {
        "sensio/generator-bundle": "~3.0", // v3.1.2
        "symfony/phpunit-bridge": "~2.7" // v2.8.15
    }
}