博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
symfony设置静态首页_使用Symfony Flex构建图像博客:设置
阅读量:2505 次
发布时间:2019-05-11

本文共 14437 字,大约阅读时间需要 48 分钟。

symfony设置静态首页

This article is part of a zero-to-hero project — a multi-image gallery blog — for performance benchmarking and optimizations. (View the here.) In this part, we’ll set our project up so we can fine tune it throughout the next few posts, and bring it to a speedy perfection.

本文是零英雄项目(一个包含多个图片的博客)的一部分,用于性能基准测试和优化。 (在此处查看 。)在这一部分中,我们将设置项目,以便我们可以在接下来的几篇文章中对其进行微调,并将其快速完善。



Now and then you have to create a new project repository, run that git init command locally and kick off a new awesome project. I have to admit I like the feeling of starting something new; it’s like going on an adventure!

有时您必须创建一个新的项目存储库,在本地运行该git init命令并启动一个新的出色项目。 我必须承认我喜欢开始新事物的感觉。 这就像在冒险!

Lao Tzu said:

老子说:

The journey of a thousand miles begins with one step

一千英里的旅程始于一步

We can think about the project setup as the very first step of our thousand miles (users!) journey. We aren’t sure where exactly we are going to end up, but it will be fun!

我们可以将项目设置视为千里(用户!)旅程的第一步。 我们不确定最终将在哪里结束,但这会很有趣!

We also should keep in mind the advice from prof. Donald Knuth:

我们还应该牢记教授的建议。 唐纳德·努斯:

Premature optimization is the root of all evil (or at least most of it) in programming.

过早的优化是编程中所有邪恶(或至少是大多数邪恶)的根源。

Our journey towards a stable, robust, high-performance web app will start with the simple but functional application — the so-called minimum viable product (MVP). We’ll populate the database with random content, do some benchmarks and improve performance incrementally. Every article in this series will be a checkpoint on our journey!

我们朝着稳定,强大,高性能的Web应用程序迈进的过程将始于简单但功能强大的应用程序-所谓的最低可行产品(MVP)。 我们将使用随机内容填充数据库,执行一些基准测试并逐步提高性能。 本系列中的每篇文章都是我们旅程中的一个检查点!

This article will cover the basics of setting up the project and organizing files for our Symfony Flex project. I’ll also show you some tips, tricks and helper scripts I’m using for speeding up the development.

本文将介绍为我们的Symfony Flex项目设置项目和组织文件的基础知识。 我还将向您展示一些我用来加快开发速度的技巧,窍门和帮助程序脚本。

我们正在建设什么? (What Are We Building?)

Before starting any project, you should have a clear vision of the final destination. Where are you headed? Who will be using your app and how? What are the main features you’re building? Once you have that knowledge, you can prepare your environment, third-party libraries, and dive into developing the next big thing.

在开始任何项目之前,您应该对最终目标有清楚的了解。 你要去哪里 谁将使用您的应用程序以及如何使用? 您要构建的主要功能是什么? 掌握了这些知识之后,就可以准备环境,第三方库,并着手开发下一个重要的东西。

In this series of articles, we’ll be building a simple image gallery blog where users can register or log in, upload images, and create simple public image galleries with descriptions written in Markdown format.

在本系列文章中,我们将构建一个简单的图片库博客,用户可以在其中注册或登录,上传图片以及创建以Markdown格式编写的描述的简单公共图片库。

We’ll be using the new and (make sure you’ve read tutorials on them, as we’re not going to cover them here). We picked Flex because Symfony 4 is just about to come out (if it hasn’t already, by the time you’re reading this), because it’s infinitely lighter than the older version and lends itself perfectly to step-by-step optimization, and it’s also the natural step in the evolution of the most popular enterprise PHP framework out there.

我们将使用新的和 (确保您已阅读有关它们的教程,因为我们不在这里介绍它们)。 我们之所以选择Flex,是因为Symfony 4即将问世(如果还没有,在您阅读本文时),因为它比旧版本轻巧得多,并且非常适合逐步优化,这也是目前最流行的企业PHP框架演进的自然步骤。

All the code referenced in this article is available at the .

这篇文章中引用的所有代码都可以在 。

We’re going to use the Twig templating engine, Symfony forms, and Doctrine ORM with UUIDs as primary keys.

我们将使用Twig模板引擎,Symfony表单和带有UUID作为主键的Doctrine ORM。

Entities and routes will use annotations; we’ll have simple , and we’ll prepare data fixtures to populate the database.

实体和路线将使用注释; 我们将进行简单 ,并准备数据夹具来填充数据库。

应用入门 (Getting started with the app)

To try out the example we’ve prepared, do the following:

要尝试我们准备的示例,请执行以下操作:

  • Set up an empty database called “blog”.

    设置一个名为“博客”的空数据库。
  • Clone the project from GitHub.

    从GitHub克隆项目 。

  • Run composer install.

    运行composer install

  • If you now open the app in your browser, you should see an exception regarding missing database tables. That’s fine, since we haven’t created any tables so far.

    如果现在在浏览器中打开应用程序,则应该看到有关缺少数据库表的异常。 很好,因为到目前为止我们还没有创建任何表。
  • Update the .env file in your project root directory with valid database connection string (i.e., update credentials).

    使用有效的数据库连接字符串(即,更新凭据)更新项目根目录中的.env文件。

  • Run the database init script ./bin/refreshDb.sh and wait until it generates some nice image galleries.

    运行数据库初始化脚本./bin/refreshDb.sh并等待,直到生成一些漂亮的图像库。

  • Open the app in your browser and enjoy!

    在浏览器中打开应用程序,然后享受!

After executing bin/refreshDb.sh you should be able to see the home page of our site:

执行bin/refreshDb.sh您应该可以看到我们网站的主页:

Project homepage

You can log in to the app with credentials user1@mailinator.com and password 123456. See fixture class for more details regarding generated users.

您可以使用凭据user1@mailinator.com和密码123456登录到该应用程序。 有关生成的用户的更多详细信息,请参见固定装置类。

从头开始 (Starting from scratch)

In this section, we’ll describe how to set up a new project from scratch. Feel free to take a look at the and see the details.

在本节中,我们将描述如何从头开始建立新项目。 随意查看并查看详细信息。

After creating a new project based on symfony/skeleton by executing the command

通过执行命令基于symfony/skeleton创建新项目后

composer create-project "symfony/skeleton:^3.3" multi-user-gallery-blog

… we can first set minimum stability to “dev” because of some cutting edge packages:

…由于一些最先进的软件包,我们首先可以将“稳定性”设置为“最低”:

composer config minimum-stability dev

… and then require additional packages (some of them are referenced by their aliases, the new feature brought by Flex):

…然后需要其他软件包(其中一些通过别名(Flex带来的新功能)进行引用):

composer req annotations security orm template asset validator ramsey/uuid-doctrine

Dependencies used only in the dev environment are required with the --dev flag:

--dev标志要求仅在开发环境中使用的--dev

composer req --dev fzaninotto/faker doctrine/Doctrine-Fixtures-Bundle

Flex is doing some serious work for us behind the scenes, and most of the libraries (or bundles) are already registered and configured with good-enough defaults! Check the config directory. You can check all the dependencies used in this project in the file.

Flex正在幕后为我们做一些认真的工作,并且大多数库(或捆绑包)已经注册并配置了足够好的默认值! 检查config目录。 您可以在文件中检查此项目中使用的所有依赖项。

Routes are defined by annotations, so the following will be automatically added into config/routes.yaml:

路由由注释定义,因此以下内容将自动添加到config/routes.yaml

controllers:  resource: ../src/Controller/  type: annotation

数据库,脚本和装置 (Database, Scripts and Fixtures)

Configure the DATABASE_URL environment variable (for example, by editing the .env file) to set up a working DB connection. If you’re using our own (recommended), you’ve got a database set up called homestead with the user / pass homestead / secret. A DB schema can be generated from existing entities by executing:

配置DATABASE_URL环境变量(例如,通过编辑.env文件)以建立有效的数据库连接。 如果您使用的是我们自己的 (推荐),则需要使用user / pass homestead / secret建立一个名为homestead的数据库。 可以通过执行以下操作从现有实体生成数据库模式:

./bin/console doctrine:schema:create

If this doesn’t run, try executing the console by invoking the PHP binary, like so:

如果此操作没有运行,请尝试通过调用PHP二进制文件来执行控制台,如下所示:

php bin/console doctrine:schema:create

If this step executed fine in the “Getting Started with the app” section above, you should be able to see newly created tables in the database (for , and entities).

如果此步骤在上面的“使用入门”部分中执行得很好,则您应该能够在数据库中看到新创建的表(对于 , 和实体)。

If you want to drop the database schema, you can run:

如果要删除数据库架构,可以运行:

./bin/console doctrine:schema:drop --full-database --force

伪造直到成功! (Fake it ’til you make it!)

I can’t imagine developing an app today without having data fixtures (i.e., scripts for seeding the DB). With a few simple scripts, you can populate your database with realistic content, which is useful when it comes to rapid app development and testing, but it’s also a requirement for a healthy CI pipeline.

我无法想象今天没有数据固定装置(即,用于播种数据库的脚本)就可以开发应用程序。 使用一些简单的脚本,您可以用实际的内容填充数据库,这对于快速的应用程序开发和测试很有用,但这也是健康的CI管道的要求。

I find the to be an excellent tool for handling data fixtures as it supports ordered fixtures (i.e., you can control the order of execution), sharing objects (via references) between scripts, and accessing the service container.

我发现是处理数据固定装置的出色工具,因为它支持有序固定装置(即,您可以控制执行顺序),在脚本之间共享对象(通过引用)以及访问服务容器。

Default Symfony services configuration doesn’t allow public access to services, as best practice is to inject all dependencies. We’ll need some services in our fixtures, so I’m going to make all services in App\Services publicly available by adding the following to config/services.yaml:

默认的Symfony服务配置不允许公共访问服务,因为最佳实践是注入所有依赖项。 我们的固定装置中将需要一些服务,因此,通过将以下内容添加到config/services.yaml ,我将使App\Services所有App\Services公开可用:

App\Service\:  resource: '../src/Service/*'  public: true

I’m also using to get random but realistic data (names, sentences, texts, images, addresses, …).

我还使用获取随机但真实的数据(名称,句子,文本,图像,地址等)。

Take a look at the with random images to get a feeling of how cool this combination is.

看一下使用随机图像为的以了解这种组合有多酷。

Usually, I combine commands for dropping the existing DB schema, creating the new DB schema, loading data fixtures, and other repetitive tasks into a single shell script — for example, bin/refreshDb.sh — so I can easily regenerate the schema and load dummy data:

通常,我将用于删除现有数据库架构,创建新数据库架构,加载数据固定装置和其他重复性任务的命令合并到一个单独的Shell脚本中(例如, bin/refreshDb.sh ,因此我可以轻松地重新生成架构并进行加载虚拟数据:

# Drop schema./bin/console doctrine:schema:drop --full-database --force# Create schema./bin/console doctrine:schema:create# Load fixtures./bin/console doctrine:fixtures:load -n --fixtures src/DataFixtures/ORM# Install assets./bin/console assets:install --symlink# Clear cache./bin/console cache:clear

Make sure you restrict execution of this script on production, or you’re going to have some serious fun at one point.

确保限制在生产环境中执行此脚本,否则您将在某一时刻获得一些乐趣。

One can argue that randomly generated data can’t reproduce different edge cases, so your CI can sometimes fail or pass depending on the data generation. It’s true, and you should make sure all edge cases are covered with your fixtures.

有人可能会争辩说,随机生成的数据不能重现不同的极端情况,因此您的CI有时会失败或通过,具体取决于数据生成。 没错,您应该确保所有边缘盒都被您的固定装置覆盖。

Every time you find an edge case causing a bug, make sure you add it to data fixtures. This will help you build a more robust system and prevent similar errors in the future.

每次发现导致错误的边缘情况时,请确保将其添加到数据固定装置中。 这将帮助您构建更强大的系统并防止将来出现类似错误。

控制器,模板和服务 (Controllers, Templates and Services)

Controller classes are located under the src\Controller directory and are not extending the existing Controller class provided by FrameworkBundle. All dependencies are injected through constructors, making the code less coupled and easier to test. I don’t like “magic” for simple stuff!

控制器类位于src\Controller目录下,并且没有扩展FrameworkBundle提供的现有Controller类。 所有依赖项都是通过构造函数注入的,从而使代码更少耦合并且更易于测试。 我不喜欢简单事物的“魔术”!

In the new Symfony directory structure, templates are located under templates. I’ve created a master template base.html.twig that defines basic HTML structure and references external resources.

在新的Symfony目录结构中,模板位于templates下。 我创建了一个主模板base.html.twig ,该模板定义了基本HTML结构并引用了外部资源。

Other templates are extending it and overriding its blocks (for example, style sheets, body, header, content, JavaScripts and other blocks).

其他模板正在扩展它并覆盖其块(例如,样式表,正文,标题,内容,JavaScript和其他块)。

Local assets are referenced by using the asset Twig function, as it will provide us with better control later (such as changing hosts and appending query strings for versioning). Other templates are organized in subdirectories within the src/templates directory.

通过使用asset Twig函数来引用本地资产,因为以后它将为我们提供更好的控制(例如更改主机和附加查询字符串以进行版本控制)。 其他模板组织在src/templates目录的子目录中。

Services are automatically registered and configured by default in Symfony’s service configuration. That way, you don’t need to manually configure Twig extensions with filters for Markdown support and generating URLs for Image entities located under src/Twig.

默认情况下,服务会自动在Symfony的服务配置中注册和配置。 这样,您无需手动配置带有过滤器的Twig扩展以支持Markdown并为位于src/Twig下的Image实体生成URL。

源代码管理/版本控制 (Source Code Management / Version Control)

Hopefully, you already know about and understand how .gitignore works.

希望您已经了解并了解.gitignore工作原理。

Symfony Flex manages the default project .gitignore by adding known bundle files and folders to the ignore list. For example, Flex would add the following files to the ignore list for symfony/framework-bundle:

Symfony Flex通过将已知的捆绑文件和文件夹添加到忽略列表来管理默认项目.gitignore 。 例如,Flex会将以下文件添加到symfony/framework-bundle的忽略列表中:

.env/public/bundles//var//vendor/

We’re going to store uploaded files in var/uploads, so we need to create it first. Instead of doing it manually on every project setup, we can add a command for creating all the needed directories to post-install-cmd event in our composer.json scripts section and make Composer run that for us (for example, "mkdir -p var/uploads"). Read more about composer scripts and auto-scripts .

我们将上传的文件存储在var/uploads ,因此我们需要先创建它。 我们可以在composer.json脚本部分中为在post-install-cmd事件中创建所有需要的目录添加命令,而不是在每个项目设置上手动执行操作,并让Composer为我们运行该命令(例如, "mkdir -p var/uploads" )。 阅读有关作曲家脚本的更多信息, 阅读有关自动脚本的更多 。

We should add all other directories and files we want to be ignored to .gitignore but outside the commented and clearly marked section managed by Flex.

我们应该将所有其他我们要忽略的目录和文件添加到.gitignore但要在由Flex管理的带有注释和清楚标记的部分之外。

其他提示和故障排除 (Other Tips and Troubleshooting)

  • Check your Homestead.yaml file for environment variables. If you already have an APP_ENV variable, Symfony won’t try to load the one from the .env file. Your best bet is to comment out the APP_ENV section in Homestead.yaml just to be safe.

    检查您的Homestead.yaml文件中的环境变量。 如果您已经有一个APP_ENV变量,Symfony将不会尝试从.env文件中加载该变量。 为了安全起见,最好的选择是注释掉Homestead.yamlAPP_ENV部分。

  • Cache clear within your Vagrant machine can fail because of permissions. You can simply run sudo rm -rf var/cache/* to clear the cache manually, or if that fails, clear it from outside the VM, on the host machine in the shared folder.

    由于权限的原因,Vagrant计算机中的缓存清除操作可能会失败。 您可以简单地运行sudo rm -rf var/cache/*手动清除缓存,或者,如果失败,请从VM外部的共享文件夹中的主机上从VM外部清除缓存。

结论 (Conclusion)

In this article, we successfully set up a project with entities, controllers, templates and helper scripts. The database is populated with dummy content, and users are able to register and create their galleries.

在本文中,我们成功地建立了带有实体,控制器,模板和帮助程序脚本的项目。 数据库中填充了虚拟内容,用户可以注册并创建他们的画廊。

In the next article, we’ll demonstrate how to populate the database with a more massive dataset to test the app’s performance in a more realistic environment, how to set up a simple testing suite, and simple CI based on Docker.

在下一篇文章中,我们将演示如何使用更庞大的数据集填充数据库以在更现实的环境中测试应用程序的性能,如何设置简单的测试套件以及基于Docker的简单CI。

翻译自:

symfony设置静态首页

转载地址:http://zxegb.baihongyu.com/

你可能感兴趣的文章
.net MVC 404错误解决方法
查看>>
linux系统目录结构
查看>>
git
查看>>
btn按钮之间事件相互调用
查看>>
Entity Framework 4.3.1 级联删除
查看>>
codevs 1163:访问艺术馆
查看>>
冲刺Noip2017模拟赛3 解题报告——五十岚芒果酱
查看>>
并查集
查看>>
sessionStorage
查看>>
代码示例_进程
查看>>
Java中关键词之this,super的使用
查看>>
人工智能暑期课程实践项目——智能家居控制(一)
查看>>
前端数据可视化插件(二)图谱
查看>>
kafka web端管理工具 kafka-manager【转发】
查看>>
获取控制台窗口句柄GetConsoleWindow
查看>>
Linux下Qt+CUDA调试并运行
查看>>
51nod 1197 字符串的数量 V2(矩阵快速幂+数论?)
查看>>
OKMX6Q在ltib生成的rootfs基础上制作带QT库的根文件系统
查看>>
zabbix
查看>>
多线程基础
查看>>