126

Short guide explaining how to migrate from Silex to Symfony 4+ Flex · Issue #867...

 6 years ago
source link: https://github.com/symfony/symfony-docs/issues/8678
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Contributor

GwendolenLynch commented on Dec 2, 2017

My First Upgrade from Silex 2 to Symfony 4 / Flex

This is just a few notes on the way I approached my first cut-over of a project from Silex to Symfony 4 & Flex. The most important thing I think I would point out for those of us very used to Silex is:

Stuff might feel "different" or "unnatural" at first, but you quickly adapt and grow attached to the change.

Before you go too far, give this a good read:

… don't try to "get" everything now, but having some of it in your mental view will help a lot of this "click" pretty quickly.

Next thing I found valuable was to composer create-project symfony/skeleton && cd skeleton && php -S 127.0.0.1:8000 -t public and have a look at what you've got … a very simple starting point.

Then I did a bunch of composer requires to start adding the external dependencies I knew I was going to require and in a few minutes I had the beginning of what I needed to start porting my Silex project, things like:

  • composer.json
    • "require"
    • "require-dev"
    • "config"
    • "scripts"
  • symfony.lock
  • bin/console
  • config/*
  • public/index.php
  • src/Kernel.php
  • .env
  • .env.dist

A huge wall you can hit at this point is copying these over, pressing F5 in your browser and watching everything crash. smile

What I chose to do at this point was to edit my config/services.yaml file and comment out this section:

    App\:
        resource: '../src/*'
        exclude: '../src/{Entity,Migrations,Repository,Tests}'
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

That stopped the DI compiler from scanning every class in src/ and causing a lot of pain trying to port your service classes over.

HUGE NOTE: This approach circumvents a lot of what makes autowiring awesome, you wouldn't normally do this on a new project, but it also allows you to slowly progress though your migration without too much pain.

Important tip, you two best friends at this point in time:

  • ./bin/console debug:container
  • ./bin/console debug:autowiring

wink

Next, I went through each of my own project's service providers and added the services one by one (as needed) to config/services.yaml

For example, a Twig extension's runtime class:

    App\Twig\Extension\MyRuntime:
        autowire: true
        tags:
            - { name: twig.runtime }

Once you have your base serviced looking like they're wired up, move onto your controllers. I enabled them in my config/services.yaml
one at a time like so:

    App\Controller\MyFirstController:
        tags: ['controller.service_arguments']

The allowed me to step through each one, get it loading and move on. Depending on dependencies you might have to jump through a few iterations of this process.

Depending on the size and complexity of your project, this should get a lot of them close to cut-over. My first one was less than two hours to migrate.

javiereguiluz, MichaelDeBoey, bobdenotter, JoseCage, rixbeck, oliveremberton, ntomka, akky, lennerd, msalsas, and 8 more reacted with thumbs up emojiandytruong and Boorj reacted with hooray emoji All reactions

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK