Getting to Know our Tiny Project
Sprint back to your command center (aka terminal). This first tab is running the web server. If you need to stop it, press Ctrl-C... then restart it with:
symfony serve
Tip
You can use symfony serve -d to run the command in the "background" so that
you can continue using this terminal tab.
We'll leave that alone and let it do its thing.
Our Project's 15 Files
Open a second terminal tab in the same directory. When we ran the symfony new command, it downloaded a tiny project and initialized a Git repository with an initial commit. That was super nice! To see our files, I'm going to open this directory in my favorite editor: PhpStorm. More on this editor in a few minutes.
Right now, I want you to notice just how small our project is! To see the full list of committed files, back at your terminal, run:
git ls-files
Yea, that's it. Only about 15 files committed to git!
Where's Symfony?
So then... where the heck is Symfony? One of our 15 files is especially important: composer.json.
| { | |
| // ... lines 2 - 5 | |
| "require": { | |
| "php": ">=8.2", | |
| "ext-ctype": "*", | |
| "ext-iconv": "*", | |
| "symfony/console": "7.0.*", | |
| "symfony/dotenv": "7.0.*", | |
| "symfony/flex": "^2", | |
| "symfony/framework-bundle": "7.0.*", | |
| "symfony/runtime": "7.0.*", | |
| "symfony/yaml": "7.0.*" | |
| }, | |
| // ... lines 17 - 70 | |
| } |
Composer is the package manager for PHP. Its job is simple: read the package names under this require key and download them. When we ran the symfony new command, it downloaded these 15 files and also ran composer install. That downloaded all of these packages into the vendor/ directory.
So where is Symfony? It's in vendor/symfony/... and we're already using about 20 of its packages!
Running Composer
The vendor/ directory is not committed to git. It's ignored thanks to another file we started with: .gitignore.
| ###> symfony/framework-bundle ### | |
| /.env.local | |
| /.env.local.php | |
| /.env.*.local | |
| /config/secrets/prod/prod.decrypt.private.php | |
| /public/bundles/ | |
| /var/ | |
| /vendor/ | |
| ###< symfony/framework-bundle ### |
This means that if a teammate clones our project, they will not have this directory. And that's okay! We can always repopulate it by running composer install.
Watch: I'll right-click and delete the entire vendor/ directory. Gasp!
If we try our app now, it's busted. Bad feels! To fix it & save the day, at your terminal, run:
composer install
And... presto! The directory is back.... and over here, the site works again.
The 2 Directories you Care About
Looking back at our files, there are only two directories that we even need to think about. The first is config/: this holds... configuration! We'll learn about what these files do along the way.
The second is src/. This is where all your PHP code will live.
And that's really it! 99% of the time you're either configuring something or writing PHP code. That happens in config/ & src/.
What about the other 4 directories? bin/ holds a single console executable file that we'll try out soon. But we're never going to look at or modify that file. The public/ directory is known as your document root. Anything you put here - like an image - will be publicly accessible. More about that stuff later. It also holds index.php.
| use App\Kernel; | |
| require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; | |
| return function (array $context) { | |
| return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']); | |
| }; |
This is known as your "front controller": it's the main PHP file that your web server executes at the start of every request. And while it is super important... you'll never edit or even think about this file.
Up next is var/. This is also ignored from git: it's where Symfony stores log files and cache files that it needs internally. So very important... but not something we need to think about. And we already talked about vendor/. That's everything!
Prepping PhpStorm
Now before we get coding, I mentioned that I use PhpStorm. You're free to use whatever editor you want. However, PhpStorm is incredible. And one big reason is the unmatched Symfony plugin. If you go to PhpStorm -> Settings and search for "Symfony", down here under Plugins and then Marketplace, you can find it. Download & install the plugin if you don't already have it. After installation, restart PhpStorm. Then there's one more step. Go back into settings and search for Symfony again. This time you'll have a Symfony section. Be sure to enable the plugin for each Symfony project you work on... otherwise you won't see all the same magic I have.
Ok! Let's start coding and build our first page in Symfony next.
8 Comments
Hello,
When I use git ls-files command it is coming empty. also git ls-files command was not working. I have started the repository with git init command.
is it important?
thank you
root@ubuntu:/var/www/html/LABPractise# git commit
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
nothing added to commit but untracked files present (use "git add" to track)
Hey Mahmut,
The
git ls-filescommand will show you only those files that are committed in your repo. If you just created a new repo withgit init- there're no committed files there. First, you need to add files you want to commit withgit addcommand and then commit changes withgit commit. After you successfully committed the changes - thegit ls-fileswill show you the output.But if you want to see the list of file in the folder (not those files that were committed to your repo) - you can just use
lsUnix command.Cheers!
What was your phpstorm theme again? :D I love how icons for files and classes look
Hey Dragos,
That's not a PhpStorm theme but a plugin, search for "Atom Material Icons" in the marketplace in case you want to have those icons :)
Cheers!
Tried everything i could google or chatgpt. No server is running, no process, deleted log folder. Still getting this error. It still works without "-d" but .... just don't like it that way.
windows 11.
Hey John,
I'm personally not on Windows so it's difficult to suggest a workaround on this. Are you running several Symfony apps at the same time? That might be the reason maybe. I suppose you installed that Symfony CLI going though the official instructions for your OS on this page: https://symfony.com/download - there are a few ways, probably try to remove and reinstall it in a different way, maybe it will be more robust via Scoop if you didn't use that yet.
Well, at least it works without -d - that's already something! And considering it's Windows, I would say you may need to get along with it. Otherwise, please consider Windows WSL where you can install Linux natively on Windows and use Linux for your coding - that would be the best solution for Windows users.
Cheers!
Maybee a little too early - but you mentioned the
/publicdirectory...Today I learned to add a
.htaccessfile into this directory when I want to upload my files to a hosting server. Easy to put in the right place withcomposer require symfony/apache-packSee more: https://symfony.com/packages/Apache Pack
In my case I could't see the Profiler (see video 07) without the .htaccess file.
Hey @skipper-henrik
Thanks for pointing it out. That package handles the
.htaccessfor you, but if you don't want to rely on that file, you can put that configuration inside your Apache virtual host fileCheers!
"Houston: no signs of life"
Start the conversation!