Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine
This tutorial has a new version, check it out!

Symfony 4: Let's Launch!

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.

Hey guys! Yes! It's Symfony 4 time! I am so excited. Why? Because nothing makes me happier than sitting down to work inside a framework where coding is actually fun, and where I can build features fast, but without sacrificing quality. Well, maybe I'd be even happier doing all of that on a beach... with, maybe a cold drink?

Anyways, Symfony 4 completely re-imagined the developer experience: you're going to create better features, faster than ever. And, Symfony has a new, unique super-power: it starts as a microframework, then automatically scales in size as your project grows. How? Stay tuned...

Oh, and did I mention that Symfony 4 is the fastest version ever? And the fastest PHP framework? Honestly, all frameworks are fast enough anyways, but the point is this: you're building on a seriously awesome foundation.

Tip

See http://www.phpbenchmarks.com for the third-party benchmark stats!

Prep: Download & Update Composer

Ok, let's get started already! Open a new terminal and move into whatever directory you want. Make sure that you already have Composer installed globally so that you can just say composer. If you have any questions, ask us in the comments!

And also make sure you have the latest version:

composer self-update

That's important: Composer had a recent bug fix to help Symfony.

Install Symfony!

To download your new Symfony project, run composer create-project symfony/skeleton and put this into a new directory called the_spacebar.

composer create-project symfony/skeleton the_spacebar '4.4.*'

That's the name of our project! "The Spacebar" will be the place for people from across the galaxy to communicate, share news and argue about celebrities and BitCoin. It's going to be amazing!

This command clones the symfony/skeleton project and then runs composer install to download its dependencies.

Further down, there's something special: something about "recipes". OooOOO. Recipes are a new and very important concept. We'll talk about them in a few minutes.

Starting the Web Server

And at the bottom, cool! Symfony gives us clear instructions about what to do next. Move into the new directory:

cd the_spacebar

Apparently, we can run our app immediately by executing:

php -S 127.0.0.1:8000 -t public

This starts the built-in PHP web server, which is great for development. public/ is the document root of the project - but more on that soon!

Tip

If you want to use Nginx or Apache for local development, you can! See http://bit.ly/symfony-web-servers.

Time to blast off! Move to your browser and go to http://localhost:8000. Say hello to your new Symfony app!

Our Tiny Project

Tip

Symfony no longer creates a Git repository automatically for you. But, no problem! Just type git init once to initialize your repository.

Back in the terminal, I'll create a new terminal tab. Symfony already inititalized a new git repository for us and gave us a perfect .gitignore file. Thanks Symfony!

Tip

If you're using PhpStorm, you'll want to ignore the .idea directory from git. I already have it ignored in my global .gitignore file: https://help.github.com/articles/ignoring-files/

That means we can create our first commit just by saying:

git init
git add .
git commit

Create a calm and well-thought-out commit message.

# Woohoo! OMG WE ARE USING SYMFONY4

Woh! Check this out: the entire project - including Composer and .gitignore stuff - is only 16 files! Our app is teenie-tiny!

Let's learn more about our project next and setup our editor to make Symfony development amazing!

Leave a comment!

72
Login or Register to join the conversation
Default user avatar

Hi there,

I have this issue :

$ cd /var/www/tests/php/symfony/sf4/the_spacebar
$ php -S 127.0.0.1:8000 public
PHP 7.2.3-1+ubuntu16.04.1+deb.sury.org+1 Development Server started at Sat Mar 17 23:13:26 2018
Listening on http://127.0.0.1:8000
Document root is /var/www/tests/php/symfony/sf4/the_spacebar
Press Ctrl-C to quit.
[Sat Mar 17 23:13:37 2018] PHP Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0
[Sat Mar 17 23:13:37 2018] PHP Fatal error: Unknown: Failed opening required 'public' (include_path='.:/usr/share/php') in Unknown on line 0

I got this error running my browser to the 127.0.0.1:8000 url (it's a blank page).
I check the permissions, the directories in vendor/, everything seems good, but... not enough to work. Why did the video run the server so easily, and not me ?

1 Reply
Default user avatar
Default user avatar Raman Paul | John | posted 4 years ago

Old thread but for new users stumble across this , it should be
`$ php -S 127.0.0.1:8000 -t public`

Switch `-t` is missing in above comment.

1 Reply

Hey Raman,

Ah, really, good catch! The "-t" option is missing :)

And thank you for pointing about it in such an old thread!

Cheers!

Reply
Hicham A. Avatar
Hicham A. Avatar Hicham A. | posted 2 years ago

hello;
how come when I run $ git status, i get this message "fatal: not a git repository (or any of the parent directories): .git"
thank you

Reply

Hey Hicham A.

That happens when you haven't initialized GIT in your project. Just run git init and follow the steps :)

Cheers!

Reply
Iryna Avatar

Hi!
I have PhpStorm 2019.3 and Symfony 5. I use build-in server (https://127.0.0.1:8000/) and Symfony Support plugin is not working correct - Symfony Profiler is empty, and no templates exist for creating Controller (but exist for Services/Yaml|XML).
How can I fix templates and profiler?

Reply

Hey Iryna

Which distribution of Symfony did you install? It seems to me that you installed the "skeleton" distribution. If what you want is a website application, you should start with the "website-skeleton" distribution, the only difference between distributions is that one comes with more packages than the other one. Check it by yourself:
- Skeleton: https://github.com/symfony/...
- Website-skeleton: https://github.com/symfony/...

Anyway, if you want the Profiler component, you will have to install it by running composer require profiler

I hope it helps. Cheers!

Reply
Dung L. Avatar
Dung L. Avatar Dung L. | posted 2 years ago

Hello,

I am not sure why but for this Symfony 4 course if I set .env file to production such as

APP_ENV=prod

- it will stop working and displayed error below, can you please tell me why? Thank you!

Oops! An Error Occurred
The server returned a "500 Internal Server Error".
Something is broken. Please let us know what you were doing when this error occurred. We will fix it as soon as possible. Sorry for any inconvenience caused.

I would like to be able set it to production mode, because I have coded in with my custom codes.

Best regards,

Reply

Hey Dung,

Make sure you clear the cache before access the page in browser. In prod environment files are not re-compiled automatically. So, try to run:
$ bin/console cache:clear --env=prod

first and then refresh the page. Does it help? If not - check "var/log/prod.log" file for more context about the error :)

Cheers!

Reply
Dung L. Avatar

Interesting I ran into

PHP Fatal error: Out of memory (allocated 834666496) in /var/www/mysite.ca/vendor/monolog/mo... on line 107

in log /var/log/apache2/error.log

To give the max I set in php.ini

memory_limit = -1

I still get the same error. I will keep digging. Please let me know if you have any advice.

Thanks Victor!

Reply

Hey Dung,

Please, keep in mind that you may have 2 php.ini: for CLI and for PHP-FPM. So, where to do changes - depends on what you're using. Also, you need to restart PHP-FPM and re-start the local web server to apply changes. Do you hit this error when executing "bin/console cache:clear"? If so, it's weird... but you can try to clear the cache manually, in your terminal just do "rm -rf var/cache/dev" or any other environment for what you want to clear the cache.

Cheers!

Reply
Dung L. Avatar
Dung L. Avatar Dung L. | Dung L. | posted 2 years ago | edited

Ok, the Error message is kinda misleading, this article helped me figure it out https://github.com/Seldaek/... that permission for apache to write to var/log/prod.log dir was not there. Not sure why but I already gave group www-data rwx permission. So i ran command

setfacl -R -m u:www-data:rwx log/

that fixed the issue, now all is working.

Thanks again Dung L. for pointing me.

Reply

Hey Dung,

Thank you for sharing this tip with others! Well, I'm not confident with this command, I mostly do things like "chown", but if it works for you - great! I'm far from Linux expert :) And yeah, permissions might be tricky sometimes :/

Cheers!

Reply
Dung L. Avatar

Sorry and please ignore all of my comments above, i was so confused by many things. The actual issue with this is because I left a dump statement in twig template hence it will load with error 500 in PROD environment! This took me down to rabbit hole. All is fixed now.

{{ dump() }}

Thank you for your time!

Reply

Hey Dung!

Ah, I see... OK, glad you figured out the problem yourself, good job! Yeah, by default that dump() works only in dev env.

Cheers!

Reply
Dung L. Avatar

Thank you for your support.

Reply
Yan patrick L. Avatar
Yan patrick L. Avatar Yan patrick L. | posted 2 years ago

Bonjour
I follow the course but I can' t install symfony 4 . It's Symfony 5 that it installed.
Can I follow all courses with symfony 5 ?
What I have to do to force composer to install symfony 4 ?
^4 to install symfony 4 generate an error.

Thanks you four tour answer.

Reply
Yan patrick L. Avatar

I found the solution.
the command below doesn't work:
composer create-project symfony/skeleton:^4 the_spacebar

I use this one :
composer create-project symfony/skeleton the_spacebar 4.0.*

Symfony4 will be installed and not symfony 5

Reply

Hey Anshen,

Ah, probably "^" symbol is a special for your terminal. I wonder what OS do you use? Because yours command does not work for my terminal, and I'm on Mac. I suppoose if you wrap the package name and its version with quotes - it shoudl fix the problem. Please, could you confirm that this command also work for you?

$ composer create-project 'symfony/skeleton:^4' the_spacebar

I think we should fix this command in script. Thanks for reporting it!

Cheers!

Reply
Default user avatar

Hey Victor,
I'm sorry to answer that now. The classes are so exciting ... I am very happy to finally learn while keeping the smile inside.

As OS, I use Windows 7 Professional and, as terminal I use Cmder.

I just tested, this command, it does not work for me :
$ composer create-project 'symfony/skeleton:^4' the_spacebar

Thank you for your reply

Cheers

Reply

Hey Anshen,

Thank you for your reply! Hm, that's weird... it just should work. What if you wrap package name with version constraint in double quotes instead? Like:
$ composer create-project "symfony/skeleton:^4" the_spacebar

Does it help? Or probably it might also depend on your Composer version. What exactly version do you have?

Cheers!

Reply
Default user avatar

Hey Victor,

it's works :

$ composer create-project "symfony/skeleton:^4" the_spacebar

My composer version it's 1.9.1

Thanks

Cheers

Reply

Hey Anshen,

Ha, nice! Thank you checking it! Then, if double quotes works for you - I'll tweak the command to use package name in double quotes, it should prevent failures for more people :)

Cheers!

Reply
Default user avatar

Victor,

This command works for me :

λ composer create-project symfony/skeleton the_spacebar 4.4.*

Reply

Hey Anshen,

Thanks for pointing to this issue! Yeah, anyway, your version of this command is correct according to the docs.

Cheers!

Reply

Hey @Anshen

Sadly, you cannot follow this tutorials by using Symfony5, it's still pretty new and the real problem are the dependencies, many of them haven't added support for Sf5.

If you are using Symfony CLI you can pass the option --version=4.4 to install version 4.4 or pass any other version you want to use

Cheers!

Reply
Yan patrick L. Avatar
Yan patrick L. Avatar Yan patrick L. | MolloKhan | posted 2 years ago

Hi @ Diego

Actually, I cannot do the lessons with symfony 5.
I installed symfony 4.0 but I could not do the course.
thanks, I installed symfony 4.4 and it's ok for me.

Cheers!

Reply

Yeah, Symfony5 is just too new still. Symfony 4.3 should work too. Cheers!

Reply

For those from Indonesia cannot play all videos from this site since Vimeo.com is totally blocked by Indonesia Goverment :(

Reply

Hey Yusuf,

We're sorry about it! Yes, we do use Vimeo for hosting our videos, but unfortunately nothing much we can do here to make it non-blocked for your country. Probably some VPN could help you to get access to it.

Cheers!

1 Reply

cannot play the video, tried to download but unsuccessful "Firefox can’t establish a connection to the server at player.vimeo.com." I have good internet connection

Reply

Hey yusuf

That's odd but it's possible that Vimeo had an outage. Could you give it another try?

Cheers!

Reply

Vimeo.com is blocked by Indonesian goverment T_T

Reply

https://vimeo.com/ is unaccesible from my ubuntu laptops

Reply

Hi I am on the different laptop and account and the result is still the same error with following message "This is a modal window.The media could not be loaded, either because the server or network failed or because the format is not supported."

Reply

Hey Yusuf,

We're sorry about it! But unfortunately nothing much we can do to unblock Vimeo in your country. I can only suggest to try some VPN servers that may bypass the blocking.

Otherwise, we do want to make our coruses accessible to everyone, that's why we have scripts below each video with exactly what we're saying in the video. Also, those scripts include dynamic expandable code blocks of the exactly what we're coding in the videos. So you can still continue reading and coding with us.

I hope this helps!

Cheers!

1 Reply

Seems like I always have to do things the hard way. I followed the tutorial and on my dev server that already has a running web server. I ran the composer create-project symfony/skeleton the_spacebar. When I went to https://my.serverurl.com/de.... I got a shock that it just listed the files in the directory. A huge red flag. Something is wrong. So, I opened the .env file instinctively. PHPStorm placed the red squiggly line under the file so I know more stuff is wrong.
I tried to find how to setup symfony on an existing web server. Found way to much stuff that don't answer my question about the .env file config. I did run the suggested Run "composer dump-env prod" to compile .env files for production use (requires symfony/flex >=1.2).
Still see directory list. I changed all the # to /. Still no joy and added TRUSTED_HOSTS='^localhost|my.url\.com$'.
How do I fix this to run properly?

Reply

Hey Sherwin,

Sure, for a real web server you need some more configuration to spin up the website. Symfony has exmple configuration for both most popular web servers: Apache and Nginx, see this doc about it: https://symfony.com/doc/cur...

I don't know what exactly web server you have, but here's a little hint for you in case you're using Apache: Install this https://github.com/symfony/... that will execute its recipe and copy/paste the .htaccess file into your public directory: https://github.com/symfony/...

Cheers!

Reply
Thomas W. Avatar
Thomas W. Avatar Thomas W. | posted 3 years ago

Video 1 post project build is incorrect (obsolete)
After the initial project build... The screen shows

What's next?

* Run your application:
1. Go to the project directory
2. Create your code repository with the git init command
3. Download the Symfony CLI at https://symfony.com/download to install a development web server

* Read the documentation at https://symfony.com/doc

Reply

Hey T. Wagner,

Could you clarify what exactly is incorrect on your opinion? This message comes from Symfony, and it depends on the version of Symfony. If you upgrade the project dependencies - the message might be a slightly different.

Cheers!

Reply
Default user avatar

hey!
i have a problem when i create a empty project, when i come to the welcome page,it's ok,the web show me,but the log tell me there is a error
"Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET /" (from "http://127.0.0.1:8000/_profiler/14e990?panel=logger")" at /my-project/vendor/symfony/http-kernel/EventListener/RouterListener.php line 139"
i try lots of ways to solve it,but still error,i am new in symfony,could u help me?
thank you so much

Reply

Hey there!

Ah, that's OK. Well, if you see the default " Welcome to Symfony" page - that's OK. It means it's the default temporary page from Symfony to get you started, but in your project there's no any route that handles "/" URL. So, when you create a route for "/" - Symfony will automatically renders *your* page instead of this default one. So, just go further in this tutorial and create a new controller for your custom homepage ;)

Cheers!

1 Reply
Default user avatar
Default user avatar Hugo Peña | posted 3 years ago

Can you tell me, which terminal are you using? I started using xampp shell, but for instance, can stop the server with ctrl c, and I had to close some windows and open new ones. I would like to use, the one you are using.

Thanks

Reply

Hey Hugo,

In the video Ryan uses iTerm2 on his Mac. I suppose you're on Windows, so, you can take a look at Git Bash, or probably a popular https://github.com/cmderdev... .

Cheers!

Reply
Default user avatar
Default user avatar Ricardo Rodriguez | posted 3 years ago

Hello and thank you for this great material but I am getting an error that says:

your requirements could not be resolved to an installable set of packages

Problem is related to facebook/webdriver 1.6.0

I wish I could upload image showing the error here, will try to do so by pasting from this URL: href="https://content.screencast....

I am running PHP 7.3 and composer 1.8.3


<img class="embeddedObject" src="https://content.screencast.com/users/RicardoRodriguezTecF/folders/Snagit/media/a1ae1ffd-6e4d-485f-9b8f-c8ea54db064e/02.06.2019-20.54.png" width="979" height="512" border="0"/>

Reply

Hey Ricardo,

Just replied to you in Twitter: https://twitter.com/bochars... . Installing curl PHP extension should fix the problem.

Cheers!

Reply
Default user avatar
Default user avatar Ricardo Rodriguez | victor | posted 3 years ago

Awesome! Thank you so much, worked like a charm.

Reply
Default user avatar
Default user avatar suleman khan | posted 3 years ago

how to update symfony its showing old version in my browser
like

Welcome to Symfony 3.4.20

Reply

Hey Suleman,

To upgrade Symfony with its dependencies - first of all you need to change version constraint for symfony/symfony in composer.json and then run "composer update". And you also need to look inside UPGRADE manuals that you can find in symfony repo: https://github.com/symfony/... - for example, here's what you need to do to upgrade from Symfony 4.1 to 4.2: https://github.com/symfony/... . Btw, we have a few tutorials where we show how to upgrade Symfony properly, you can take a look at:
https://symfonycasts.com/sc...
https://symfonycasts.com/sc...

P.S. Also, keep in mind that new Symfony 4 requires PHP 7.1.3, if you have a lower version of your PHP - you won't be able to upgrade, upgrade your PHP first.

Cheers!

Reply
Dennis E. Avatar
Dennis E. Avatar Dennis E. | posted 3 years ago

Hey Ryan,

When I run the command "composer create-project symfony/skeleton name_of_project" it installs version 3.4.20.
How is this possible and how can I upgrade to symfony 4?

Best regards,
Dennis

P.S. I did run composer self-update before installing the project.

Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": "^7.1.3",
        "ext-iconv": "*",
        "sensio/framework-extra-bundle": "^5.1", // v5.1.3
        "symfony/asset": "^4.0", // v4.0.3
        "symfony/console": "^4.0", // v4.0.14
        "symfony/flex": "^1.0", // v1.17.6
        "symfony/framework-bundle": "^4.0", // v4.0.14
        "symfony/lts": "^4@dev", // dev-master
        "symfony/twig-bundle": "^4.0", // v4.0.3
        "symfony/web-server-bundle": "^4.0", // v4.0.3
        "symfony/yaml": "^4.0" // v4.0.14
    },
    "require-dev": {
        "easycorp/easy-log-handler": "^1.0.2", // v1.0.4
        "sensiolabs/security-checker": "^5.0", // v5.0.3
        "symfony/debug-bundle": "^3.3|^4.0", // v4.0.3
        "symfony/dotenv": "^4.0", // v4.0.14
        "symfony/monolog-bundle": "^3.0", // v3.1.2
        "symfony/phpunit-bridge": "^3.3|^4.0", // v4.0.3
        "symfony/profiler-pack": "^1.0", // v1.0.3
        "symfony/var-dumper": "^3.3|^4.0" // v4.0.3
    }
}