Login to bookmark this video
02.

Meet our Tiny App + PhpStorm Setup

|

Share this awesome video!

|

One of my main goals in these tutorials will be to help you really understand how Symfony - how your application - works.

To start with that, let's take a quick look at the directory structure.

The public/ Directory

There are only three directories you need to think about. First, public/ is the document root: so it will hold all files that need to be accessible by a browser. And... there's only one right now: index.php:

28 lines | public/index.php
// ... lines 1 - 2
use App\Kernel;
use Symfony\Component\ErrorHandler\Debug;
use Symfony\Component\HttpFoundation\Request;
require dirname(__DIR__).'/config/bootstrap.php';
if ($_SERVER['APP_DEBUG']) {
umask(0000);
Debug::enable();
}
if ($trustedProxies = $_SERVER['TRUSTED_PROXIES'] ?? $_ENV['TRUSTED_PROXIES'] ?? false) {
Request::setTrustedProxies(explode(',', $trustedProxies), Request::HEADER_X_FORWARDED_ALL ^ Request::HEADER_X_FORWARDED_HOST);
}
if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? $_ENV['TRUSTED_HOSTS'] ?? false) {
Request::setTrustedHosts([$trustedHosts]);
}
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

This is called the "front controller": a fancy word that programmers invented to mean that this is the file that's executed by your web server.

But, really, other than putting CSS or image files into public/, you'll almost never need to think about it.

src/ and config/

So... I kinda lied. There are truly only two directories that you need to think about: config/ and src/. config/ holds... um... puppies? No, config/ holds config files and src/ is where all your PHP code will go. It's just that simple.

Where is Symfony? Our project started with a composer.json:

66 lines | composer.json
{
"type": "project",
"license": "proprietary",
"require": {
"php": "^7.2.5",
"ext-ctype": "*",
"ext-iconv": "*",
"symfony/console": "5.0.*",
"symfony/dotenv": "5.0.*",
"symfony/flex": "^1.3.1",
"symfony/framework-bundle": "5.0.*",
"symfony/yaml": "5.0.*"
},
"require-dev": {
},
// ... lines 16 - 64
}

file, which lists all the third-party libraries that our app requires. Behind the scenes, that symfony new command used composer to install these... which is a fancy way of saying that Composer downloaded a bunch of libraries into the vendor/ directory... including Symfony itself.

We'll talk more about the other files and directories along the way... but they're not important yet.

Using the Symfony Local Web Server

A few minutes ago, we used PHP itself to start a local web server. Cool. But hit Ctrl+C to quit that. Why? Because that handy symfony binary tool we installed comes with a more powerful local server. Run:

symfony serve

That's it. The first time you run this, it may ask you about installing a certificate. That's optional. If you do install it - I did - it will start the web server with https. Yep, you get https locally with zero config.

Once it's running, move over to your browser and refresh. It works! And the little lock icon proves that we're now using https.

To stop the web server, just hit Control + C. You can see all of this command's options by running:

symfony serve --help

Like ways to control the port number. When I use this command, I usually run:

symfony serve -d

The -d means to run as a daemon. It does the exact same thing except that now it runs in the background... which means I can still use this terminal. Running:

symfony server:status

Shows me that the server is running and:

symfony server:stop

Will stop it. Let's start it again:

symfony serve -d

Installing PhpStorm Plugins

Ok: we're about to start doing a lot of coding... so I want to make sure your editor is ready to go. And, yea, you can use whatever editor your want. But I highly recommend PhpStorm! Seriously, it makes developing in Symfony a dream! And no, the nice people at PhpStorm aren't paying me to say this... though... they do actually sponsor several open source PHP devs... which is kinda better.

To really make PhpStorm awesome, you need to do two things. First, open the Preferences, select "Plugins" and click "Marketplace". Search for "Symfony".

This plugin is amazing... proven by the nearly 4 million downloads. This will give us all kinds of extra auto-completion & intelligence for Symfony. If you don't have it already, install it. You should also install the "PHP Annotations" and "PHP toolbox" plugins. If you search for "php toolbox"... you can see all three of them. Install them and restart PhpStorm.

Once you've restarted, go back to Preferences and Search for Symfony. In addition to installing the plugin, you also need to enable it on a project-by-project basis. Check Enable and then apply. It says you need to restart PhpStorm... but I don't think that's true.

The second thing you need to do in PhpStorm is to search for Composer and find the "Languages and Frameworks", "PHP", "Composer" section. Make sure the "Synchronize IDE settings with composer.json" box is checked... which automatically configures several useful things.

Hit "Ok" and... we are ready! Let's create our very first page and see what Symfony is all about, next.