New in Symfony: the UX initiative, a new JavaScript ecosystem for Symfony

Since its creation, JavaScript has always been focused on creating innovative User Experiences. It lets developers create the most intuitive and usable User Experience possible for a specific problem. In practice however, building great User Experiences with JavaScript is difficult. It takes time to choose reliable packages, to configure them, to integrate them in your pages, and to make your front-end code interact with the rest of your infrastructure. This problem is not new: it looks a whole lot like the state of Symfony in PHP before Symfony Flex. We need a Symfony Flex equivalent for JavaScript: a tool to build amazing User Experiences as quickly as we can now setup an HTTP client, a Mailer or an administration panel. That’s Symfony UX.

Symfony UX: building highly interactive applications by leveraging JavaScript giants¶ Symfony UX is a series of tools to create a bridge between Symfony and the JavaScript ecosystem. It stands on the shoulders of JavaScript giants: npm, Webpack Encore and Stimulus. Symfony UX is not tied to any specific JavaScript framework: you can still use React, Vue or Angular if you love them. It is also opinionated, based on standard HTML and can be progressively adopted in existing applications. Symfony UX consists of three main components:

a Symfony integration for Stimulus to provide a new organization for JavaScript code in applications updates to Symfony Flex and Symfony Webpack Encore to automatically configure JavaScript code shipped by PHP packages a series of core UX packages designed to be reliable and composable to create amazing interfaces quickly

Example: displaying charts in PHP using Symfony UX Chart.js¶ A good example of the power of Symfony UX is the new Symfony UX Chart.js component. This component relies on the Chart.js library internally. To use Symfony UX, first update your Symfony Flex and Webpack Encore dependencies: 1 2composer update symfony/flex yarn upgrade "@symfony/webpack-encore@^0.32.0"

And synchronize your recipes to the latest version: 1composer recipes:install symfony/webpack-encore-bundle --force -v

Note Synchronizing your recipes may override some code of your application. Check manually the result of the command to choose what to keep!

Once updated, Symfony Flex will react to each PHP package you install that contains JavaScript code. For instance, you can now install the Chart.js component: 1$ composer require symfony/ux-chartjs

1 2 3 4 5 6Using version ^1.0 for symfony/ux-chartjs ./composer.json has been updated

Synchronizing package.json with PHP packages Don't forget to run npm or yarn to refresh your Javascript dependencies! ...

Symfony Flex has just synchronized the package.json file of your project with the Chart.js PHP package you installed. You will now find in this file a new JavaScript module: 1 2 3 4 5 6 7// package.json { "devDependencies": { ... "@symfony/ux-chartjs": "file:vendor/symfony/ux-chartjs/Resources/assets" } }

Symfony Flex also updated a new assets/controllers.json file in your project. This file references all the Stimulus controllers provided by installed Symfony UX packages to let Webpack Encore add them to your JavaScript built files: 1 2 3 4 5 6 7 8 9 10 11 12// assets/controllers.json { "controllers": { "@symfony/ux-chartjs": { "chart": { "enabled": true, "webpackMode": "eager" } } }, "entrypoints": [] }

Because of these changes, you should now install the new JavaScript dependencies and compile the new files: 1 2yarn install --force yarn encore dev

And… that’s it! By leveraging Symfony Flex, Symfony UX Chart.js was installed and configured both as a Symfony bundle in PHP, and as a JavaScript library compiled into your application’s built files. This means you can now build a chart using the bundle: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31// ... use Symfony\UX\Chartjs\Builder\ChartBuilderInterface; use Symfony\UX\Chartjs\Model\Chart;

class HomeController extends AbstractController { /**

  • @Route("/", name="homepage") */ public function index(ChartBuilderInterface $chartBuilder): Response { $chart = $chartBuilder->createChart(Chart::TYPE_LINE); $chart->setData([ 'labels' => ['January', 'February', 'March', 'April', 'May', 'June', 'July'], 'datasets' => [ [ 'label' => 'My First dataset', 'backgroundColor' => 'rgb(255, 99, 132)', 'borderColor' => 'rgb(255, 99, 132)', 'data' => [0, 10, 5, 2, 20, 30, 45], ], ], ]);

    $chart->setOptions([/* ... */]);
    
    return $this->render('home/index.html.twig', [
        'chart' => $chart,
    ]);

    } }

All options and data are provided as-is to Chart.js. You can read Chart.js documentation to discover them all. Discover all the packages of Symfony UX on github.com/symfony/ux!

Let’s build an amazing ecosystem together¶ Symfony UX is an initiative: its aim is to build an ecosystem. To achieve this, we need your help: what other packages could we create in Symfony UX? What about a library that automatically adds an input mask to the text fields of your Symfony forms? Or the ability to make the EntityType render with AJAX auto-completion? Anything you do in JavaScript could be done streamlined as a UX package. We have some ideas and we will release more packages in the coming days. The rest is on us: let’s create an amazing ecosystem together!

                Sponsor the Symfony project.

http://feedproxy.google.com/~r/symfony/blog/~3/fLKQpwmPT6o/new-in-symfony-the-ux-initiative-a-new-javascript-ecosystem-for-symfony

Created 4y | Dec 3, 2020, 11:20:11 AM


Login to add comment

Other posts in this group

SymfonyLive Paris 2025 : Du lego de composants pour un bundle Gotenberg !

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

Feb 24, 2025, 1:50:07 PM | Symfony
A Week of Symfony #947 (17-23 February 2025)

This week, development activity focused on new security features. The upcoming Symfony 7.3 version added support for security voters to explain their vote, improved the IsGranted attribute to allow us

Feb 23, 2025, 10:10:09 AM | Symfony
SymfonyLive Berlin 2025: Agentic Applications with Symfony

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

We’re thrilled to announce

Feb 21, 2025, 9:20:12 AM | Symfony
SymfonyLive Paris 2025 : Postgres pour vos besoins NoSQL

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

Feb 20, 2025, 10:10:13 AM | Symfony
SymfonyLive Berlin 2025: Asynchronous PHP

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

We’re thrilled to announce

Feb 19, 2025, 8:40:05 AM | Symfony
SymfonyLive Paris 2025 :  Le Composant Symfony Mapper

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

Feb 18, 2025, 2:10:24 PM | Symfony
A Week of Symfony #946 (10-16 February 2025)

This week, development activity focused on the upcoming Symfony 7.3 version. We introduced a simpler way to configure DKIM and SMIME options, improved how to work with value objects in the container a

Feb 16, 2025, 11:10:08 AM | Symfony