gstreamer0.10-ffmpeg
gstreamer0.10-plugins-good
packages.
We've got a big project today, a site about movies. But only movies that have Samuel L. Jackson.
We'll start by creating a MovieController
. We could right click on the
Controller
directory, go to new, and select file from the menu. But we can
be faster! For mac users use ⌘+n
to get the "new" menu open. And because
we have the Symfony Plugin, by typing 'controller' you'll find an option to create
one directly. Let's keep things simple and obvious and call it MovieController
.
namespace AppBundle\Controller; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
class MovieController extends Controller | |
{ | |
public function indexAction($name) | |
{ | |
return $this->render('', array('name' => $name)); | |
} | |
} |
Ah, and when you use Git, PHPStorm asks you whether you want to add new files to git. I prefer to just do this myself so I'll click 'no'. Beautiful!
We'll need a route to this MovieController
, so add some annotations above indexAction()
.
Let's change these to @Route
and let autocomplete do its thing. The one we want is
Sensio\Bundle\FrameworkExtraBundle\Configuration\Route
, select that option and hit
tab and notice that it added a use
statement on line 5:
... lines 1 - 4 | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
... lines 6 - 7 | |
class MovieController extends Controller | |
{ | |
/** | |
* @Route("/movies/new") | |
*/ | |
public function newAction() | |
... lines 14 - 16 | |
} |
That's really important because when you use annotations you need to have a use
statement
for them. Now instead of spending your time looking it up in the documentation, autocomplete
will take care of it for you. Robots FTW!
Let's have our path be /movies/new
and change indexAction()
to newAction()
because
this controller will render a "new movie" form. And we'll fix up our render call in
a second:
... lines 1 - 9 | |
/** | |
* @Route("/movies/new") | |
*/ | |
public function newAction() | |
{ | |
return $this->render('', array()); | |
} | |
... lines 17 - 18 |
Head over to the browser and check if our new route is hitting our endpoint. Perfect! It can't find the template but that's fine because, there isn't one :)
Annotations have a lot of options and one of them here is called name
, which auto-completes
for us. Hey thanks. Fill that in with movies_new
:
... lines 1 - 9 | |
/** | |
* @Route("/movies/new", name="movies_new") | |
*/ | |
public function newAction() | |
... lines 14 - 18 |
If you're the curious sort and are wondering what other options you have, just hold the control key and hit the space bar: that will bring up all the options that you have in that spot. In fact, "control+space" can be used pretty much anywhere to show you your auto-complete option - really handy.
This autocomplete works for two reasons. First, an annotation represents a real class.
Hold command, or control if you're in windows, and click @Route
to open up the
class that fuels it.
... lines 1 - 11 | |
namespace Sensio\Bundle\FrameworkExtraBundle\Configuration; | |
... lines 13 - 19 | |
class Route extends BaseRoute | |
{ | |
... lines 22 - 43 | |
} |
If you hold command again and go into its base class, you'll see that all of those options are represented as properties inside of that class:
... lines 1 - 11 | |
namespace Symfony\Component\Routing\Annotation; | |
... lines 13 - 21 | |
class Route | |
{ | |
private $path; | |
private $name; | |
private $requirements = array(); | |
private $options = array(); | |
private $defaults = array(); | |
private $host; | |
private $methods = array(); | |
private $schemes = array(); | |
private $condition; | |
... lines 33 - 181 | |
} |
To take this further if you go back to MovieController
you can hold command and
click on the name
option and have it take you right to that property. You may not
need this very often, but sometimes it's useful to see how an annotation option is
used to figure out what value you want to set for it.
Now thanks to the annotations plugin, annotations act a lot more like normal code, with fancy auto-completion and other goodies.
// 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
}
}