symfony

tech2022-08-22  135

symfony

Symfony Flex is a modern replacement for the Symfony Installer, and not the name of the next Symfony version. As the intro text says:

Symfony Flex是Symfony Installer的现代替代品,而不是下一Symfony版本的名称。 正如介绍性文字所述:

Internally, Symfony Flex is a Composer plugin that modifies the behavior of the require and update commands. When installing or updating dependencies in a Flex-enabled application, Symfony can perform tasks before and after the execution of Composer tasks.

在内部,Symfony Flex是Composer插件,可修改require和update命令的行为。 在启用Flex的应用程序中安装或更新依赖项时,Symfony可以在执行Composer任务之前和之后执行任务。

The new Symfony will be called just Symfony 4, and while this tutorial will deal only with the Flex tool, it will mention some Symfony 4 upgrades as well.

新的Symfony将仅称为Symfony 4,尽管本教程仅涉及Flex工具,但也会提到一些Symfony 4升级。



仍在开发中 (Still Under Development)

Symfony Flex can be considered a Composer wrapper, in that it provides your Symfony project with additional options during installation and configuration. It was developed with simplicity in mind, and obviously heavily influenced by the user-friendliness of Laravel. Remember, Laravel got to its current level of popularity due to its ease of use and the low entry barrier it provides newcomers with, and Symfony wanted to emulate this.

Symfony Flex可以被视为Composer包装器,因为它在安装和配置过程中为您的Symfony项目提供了其他选项。 它的开发考虑了简单性,并且显然受到Laravel的用户友好性的影响。 请记住,Laravel由于其易用性和为新手提供的低进入门槛而达到了当前的流行水平,Symfony希望效仿这一点。

It should be noted that both Flex and Symfony 4 are still under development, slated for release somewhere at the end of November this year (2017). As such, some of the features mentioned in this post may have changed by the time you read it, but we’ll do our best to keep it up to date.

应当指出,Flex和Symfony 4仍在开发中,计划于今年11月底(2017年)发布。 因此,阅读本文时,本文中提到的某些功能可能已更改,但我们会尽力使它保持最新状态。

Most notably, the use of a makefile and the make tool to build a project if Symfony/Console is unavailable is still up in the air, as it seems to not be working properly on some operating systems. Fabien recently held a survey around this, asking for the community’s suggestions to a replacement, and overwhelmingly, the community voted in favor of just making Symfony/Console required.

最值得注意的是,如果无法使用Symfony / Console ,则仍然需要使用makefile和make工具来构建项目,因为在某些操作系统上它似乎无法正常工作。 Fabien最近对此进行了一项调查 ,征询了社区对替代产品的建议,并且绝大多数情况下,社区投票赞成仅要求Symfony / Console。

有什么不同? (What’s Different?)

Most notably, Flex respects the coming Symfony 4 updates which boil down to the following major changes:

最值得注意的是,Flex尊重即将到来的Symfony 4更新,该更新归结为以下主要变化:

PHP 7+ is required

需要PHP 7+

all folders are optional. If your project isn’t using one, it doesn’t have to be there. This makes the directory tree much simpler and more readable. Additionally, often useless files like .htaccess, LICENSE, and README have been removed as well – a project which needs those can easily add them.

所有文件夹都是可选的。 如果您的项目不使用一个,则不必在那里。 这使目录树更加简单易读。 此外,通常还删除了.htaccess , LICENSE和README类的无用文件–需要这些文件的项目可以轻松添加它们。

there is no more web folder. Instead, there is the public folder, like in all other major frameworks. This consolidates user experience across ecosystems.

没有更多的web文件夹。 取而代之的是在public文件夹,就像所有其他的主要框架。 这巩固了整个生态系统的用户体验。

temporary files go under /var in the root of the project folder, with the /var/cache subfolder reserved for long term cache, like merged class files for deploying apps as read-only artifacts

临时文件位于项目文件夹根目录下的/var下,其中/var/cache子文件夹保留用于长期缓存,例如用于将应用程序部署为只读工件的合并类文件

source code goes under /src. No /app.

源代码位于/src 。 否/app 。

configuration goes into /config.

配置进入/config 。

templates go into /templates.

模板进入/templates 。

Flex will have its own Symfony-verified list of packages that are referenced by one and one alias alone. So executing composer require cli will actually trigger Flex, which will look in its list of packages, find the one tagged as cli (in this case, Symfony Console), and install it. These “official” packages are called recipes, and can be found here. To accept user-submitted recipes, a flag exists in Flex’s configuration which needs to be set to true: composer config extra.symfony.allow-contrib true. Those recipes can be found here. By officially endorsing some packages, Symfony is in many ways becoming as opinionated as Laravel. While this is bad in some ways, it’s very good in many more ways: a consolidated, opinionated way to build Symfony apps used by most people so that everyone is on the same page.

Flex将拥有自己的经过Symfony验证的软件包列表,这些列表仅由一个和一个别名引用。 因此,执行composer require cli实际上会触发Flex,Flex将在其软件包列表中查找,找到标记为cli的软件包(在本例中为Symfony Console)并进行安装。 这些“官方”软件包称为配方,可以在此处找到。 要接受用户提交的配方,Flex的配置中存在一个标记,需要将该标记设置为true: composer config extra.symfony.allow-contrib true 。 这些食谱可以在这里找到。 通过正式认可某些软件包,Symfony在许多方面都变得像Laravel一样自以为是。 尽管这在某些方面是不好的,但在其他方面却是非常好的:一种综合的,固执己见的方式来构建大多数人使用的Symfony应用程序,以便每个人都在同一页面上。

bundle fragments no longer need to be custom-activated and added into a ton of files. Flex automates this, as well as their removal.

捆绑包片段不再需要自定义激活并添加到大量文件中。 Flex会自动执行此操作以及删除操作。 instead of parameters in config files, Symfony 4 will be using environment variables like Laravel

代替配置文件中的参数,Symfony 4将使用Laravel之类的环境变量

自举 (Bootstrapping)

As usual, we’ll assume you’re already running a healthy VM environment like Homestead Improved so you can follow along.

与往常一样,我们假设您已经在运行Homestead Improvement这样的健康VM环境,因此您可以继续进行下去。

Okay, let’s get our hands dirty with an example app. All Symfony apps can now be started from the bare bones super-minimal Symfony Skeleton app:

好的,让我们开始尝试一个示例应用程序。 现在,所有Symfony应用程序都可以从裸露的超最小Symfony Skeleton应用程序启动:

composer create-project symfony/skeleton flexy

Notice the created directory structure.

注意创建的目录结构。

In /public, we no longer have app.php and app_dev.php, only the index.php file. The type of the environment (test / dev / prod) is now dictated with environment variables, and reads the configuration from the /config folder.

在/public ,我们不再有app.php和app_dev.php ,只有index.php文件。 现在,环境类型(test / dev / prod)由环境变量决定,并从/config文件夹中读取配置。

Notice how the end of the installation process mentions that make cache-warmup was called, and that you can run make serve. This is where the new Symfony uses the “controversial” Makefile approach mentioned above. This might change.

请注意,安装过程结束时如何提到调用make cache-warmup ,并且可以运行make serve 。 这就是新的Symfony使用上面提到的“有争议的” Makefile方法的地方。 这可能会改变。

Out of the box, opening this skeleton in the browser will throw an error because no routes have been defined yet. Let’s fix this.

开箱即用,在浏览器中打开此骨架将引发错误,因为尚未定义任何路由。 让我们解决这个问题。

index: path: / defaults: { _controller: 'App\Controller\DefaultController::index' }

config/routes.yaml

config/routes.yaml

We’ll need to create this controller and its index action:

我们需要创建此控制器及其index操作:

<?php namespace App\Controller; use Symfony\Component\HttpFoundation\Response; class DefaultController { public function index() { return new Response('Hello'); } }

This will produce a simple Hello screen, like so:

这将产生一个简单的Hello屏幕,如下所示:

执行权限 (Execution Permissions)

If you try to install a binary like the Symfony/Console with composer req cli, you might run into the following problem:

如果尝试使用composer req cli安装像Symfony / Console这样的二进制文件,则可能会遇到以下问题:

~ bin/console -bash: bin/console: Permission denied

This is a known hiccup when using virtual machines, and can be easily fixed by either:

使用虚拟机时,这是一个已知的问题,可以通过以下任一方式轻松解决:

running the console with php bin/console instead of running it directly, or

使用php bin/console运行php bin/console而不是直接运行,或者

adding the “execute” permission to the file on the host machine (not from within the virtual machine), by executing: chmod +x bin/console. This will allow the direct execution of bin/console from within the VM then.

通过执行chmod +x bin/console将“执行”权限添加到主机上的文件(而不是从虚拟机内部)。 然后,这将允许从VM内部直接执行bin/console 。

添加捆绑 (Adding Bundles)

That “Hello” view we built is kind of naked. Let’s add some templates into the mix.

我们建立的“ Hello”视图有点裸露。 让我们添加一些模板。

composer req template

We can use template, twig, templates, or templating here, as defined in the Twig recipe’s aliases.

我们可以在此处使用Twig配方的别名中定义的template , twig , templates或templating 。

The Symfony 4 / Flex approach will automatically activate this bundle for us and set up a folder (/templates) with a base layout view, as well as a configuration file (config/packages/twig.yaml).

Symfony 4 / Flex方法将自动为我们激活此捆绑包,并使用base布局视图以及配置文件( config/packages/twig.yaml )设置一个文件夹( /templates )。

We are now free to define a view for our Hello route:

现在,我们可以为Hello路线定义视图:

{% extends '../base.html.twig' %} {% block body %} {{ greeting }} {% endblock %}

/templates/default/index.html.twig

/templates/default/index.html.twig

Now we can modify the controller to return this instead of the plain text response:

现在,我们可以修改控制器以返回此值而不是纯文本响应:

<?php namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class DefaultController extends Controller { public function index() { return $this->render('default/index.html.twig', ['greeting' => 'hello']); } }

Notice how we had to extend the FrameworkBundle‘s controller to get access to the render method, but that was about all the added configuration we had to do. Our hello route is now way cooler.

请注意,我们必须扩展FrameworkBundle的控制器才能访问render方法,但这就是我们要做的所有添加的配置。 我们的问候路线现在变得凉爽一些。

大捆绑 (Big Bundles)

Now let’s try adding in a big bundle – one that includes several others. The admin bundle for creating backends is a good option. Besides, it’s one the Symfony team decided to officially endorse, and it pulls in the orm recipe, which refers to Doctrine – another Symfony recommendation (can you see the opinionatedness in action?)

现在,让我们尝试添加一个大捆绑包-其中包括多个捆绑包。 创建后端的admin包是一个不错的选择。 此外,这是Symfony团队决定正式认可的一个,它引入了orm配方,这是Symfony的另一项建议(指的是Doctrine)(您能看到行动中的自以为是)

composer req admin

We’ll need to create an entity before we can use the admin bundle. For that, we need a database. Create a new database and user. This process should be fine:

我们需要先创建一个实体,然后才能使用管理包。 为此,我们需要一个数据库。 创建一个新的数据库和用户。 这个过程应该很好:

mysql -u homestead -psecret create database flexy character set utf8mb4 collate utf8mb4_unicode_ci;

Feel free to create a database specific user too if you feel it’s necessary. Then, modify the .env file to respect this:

如果您认为有必要,也可以随意创建数据库特定的用户。 然后,修改.env文件以遵守此要求:

DATABASE_URL="mysql://homestead:secret@127.0.0.1:3306/flexy?charset=utf8mb4&serverVersion=5.7"

Finally, let’s create an entity. Suppose we’re making a site that lets users make submissions to the site – like Reddit, links to submit for example. We’ll have an entity called “Submission”, like so:

最后,让我们创建一个实体。 假设我们正在创建一个站点,该站点允许用户向该站点提交内容,例如Reddit,即提交链接。 我们将有一个名为“ Submission”的实体,如下所示:

<?php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; /** * Class Submission * @package App\Entity * * @ORM\Entity * @ORM\Table(name="submission") */ class Submission { /** * @ORM\Column(type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ public $id; /** * @ORM\Column(type="string", length=255) */ public $name; /** * @ORM\Column(type="string", length=255) */ public $url; /** * @ORM\Column(type="text") */ public $description; }

The entity needs to be registered in the config/packages/easy_admin.yml file:

该实体需要在config/packages/easy_admin.yml文件中注册:

easy_admin: entities: - App\Entity\Submission

Now we’ll have Doctrine create this table for us.

现在,我们将使Doctrine为我们创建此表。

bin/console doctrine:schema:update --force

Note that you can also have Doctrine create the database as well if it doesn’t exist. Look into doctrine:database:create for that functionality.

请注意,如果数据库不存在,您也可以让Doctrine创建数据库。 查看doctrine:database:create以实现该功能。

If we now visit the /admin URL of our app, we should see something like this:

如果现在访问应用程序的/admin URL,则应该看到类似以下内容的内容:

Adding submissions should now work like a charm:

现在,添加提交应该看起来很吸引人:

非官方捆绑 (Unofficial Bundles)

What about bundles that aren’t officially backed by Symfony?

没有Symfony正式支持的捆绑软件呢?

For that, we need to flip a Composer flag’s switch.

为此,我们需要翻转Composer标志的开关。

composer config extra.symfony.allow-contrib true

This will allow for pulling of recipes from this repository as well. Let’s say we want to make our submissions have uuid for ID instead of a simple auto-incrementing integer. We can use Ramsey’s UUID-Doctrine bundle for that. When requesting contrib recipes, they typically don’t have aliases and have to be referenced in full, like regular packages.

这也将允许从该存储库中提取配方。 假设我们要使提交的ID具有uuid ,而不是简单的自动递增整数。 我们可以为此使用Ramsey的UUID-Doctrine捆绑包。 当请求贡献食谱时,它们通常没有别名,必须像常规软件包一样被完整引用。

composer req ramsey/uuid-doctrine

Since this is a community contributed package, Symfony will throw a warning at you once it’s done downloading it.

由于这是社区贡献的软件包,因此Symfony下载完成后会向您发出警告。

Note: The contrib repo is a great filter of dead bundles and packages – every bundle/package developer who cares about their work will have moved it there, so you can be sure that the development on the missing ones has stagnated.

注意:contrib存储库可以很好地过滤掉死掉的捆绑软件和软件包-每个关心他们工作的捆绑软件/软件包开发人员都会将其移到那里,因此您可以确定丢失的软件包/软件包的开发已经停滞了。

Once the package has been installed, we can use it in our project.

安装软件包后,我们可以在项目中使用它。

First, we need to tell Doctrine that it’s now available (something the recipe should do by itself, in my opinion – not automated enough just yet!):

首先,我们需要告诉Doctrine它现在可用(我认为该食谱应自行完成–尚未足够自动化!):

doctrine: dbal: url: '%env(DATABASE_URL)%' types: uuid: Ramsey\Uuid\Doctrine\UuidType orm: ...

config/packages/doctrine.yaml

config/packages/doctrine.yaml

Next, we change the Submission entity to use this type on its id attribute:

接下来,我们将Submission实体更改为在其id属性上使用此类型:

... class Submission { /** * @var \Ramsey\Uuid\Uuid * * @ORM\Id * @ORM\Column(type="uuid", unique=true) * @ORM\GeneratedValue(strategy="CUSTOM") * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator") */ public $id; ...

Now let’s update the database and clear current entities:

现在,让我们更新数据库并清除当前实体:

bin/console doctrine:schema:drop --force bin/console doctrine:schema:update --force

Finally, let’s try re-visiting /admin and adding a new entity.

最后,让我们尝试重新访问/admin并添加一个新实体。

Sure enough, our new entity has a UUID for a primary key.

果然,我们的新实体具有一个主键的UUID。

Note: it is recommended to use another type when using UUID for primary keys in InnoDB type databases, but for the sake of brevity we used the default. Full instructions here.

注意:在InnoDB类型数据库中将UUID用作主键时,建议使用另一种类型,但是为了简洁起见,我们使用默认类型。 完整说明在这里 。

添加第三方工具 (Adding Third Party Tools)

Other third party tools can be used just like before – only they won’t be auto-configurable by Flex. You’ll have to register them manually and remove them in the same way. It is therefore recommended you move any package that needs extra configuration to work smoothly with Symfony into the contrib recipes repo, so that others may benefit from the smoother Flex workflow.

其他第三方工具可以像以前一样使用–只有Flex无法自动配置它们。 您必须手动注册它们,并以相同的方式删除它们。 因此,建议您将需要额外的配置工作顺利,Symfony的入任何包contrib食谱回购,以便其他人可以从平滑的Flex工作流程中受益。

结论 (Conclusion)

Symfony Flex is the modern way to install and manage Symfony apps, and it’s the red carpet towards the door of Symfony 4. Needless to say, we’re very excited about Symfony’s most recent foray into modern development and the field of high-DX, and we’ll be keeping a close eye on it. We hope you found this introduction useful!

Symfony Flex是安装和管理Symfony应用程序的现代方式,它是Symfony 4大门的红地毯。不用说,我们对Symfony在现代开发和高DX领域的最新尝试感到非常兴奋,我们将密切关注。 我们希望您对本介绍很有帮助!

We didn’t benchmark this final result with another framework purely because it makes no sense when you consider the possible optimizations you can do, but also because we’ve got a performance month coming up (all of November) during which we’ll devour all sorts of performance related issues and go through a whole set of techniques you’ll be able to apply to your projects – no matter the framework or app type. Stay tuned!

我们之所以没有使用另一个框架来对最终结果进行基准测试,纯粹是因为当您考虑可以进行的优化时没有意义,而且还因为我们要花一个性能月(整个11月),各种与性能相关的问题,并通过一整套技术可以将其应用于您的项目-无论是框架还是应用程序类型。 敬请关注!

翻译自: https://www.sitepoint.com/symfony-flex-paving-path-faster-better-symfony/

symfony

相关资源:symfony_emp_title:面试任务-源码
最新回复(0)