概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:31:24 | 236 次阅读 | 评论: 0 | 来源: 网络整理

安装Symfony

In the past, Symfony projects were created with Composer, the dependency manager for PHP applications. However, the current recommendation is to use the Symfony Installer, which has to be installed before creating your first project.

Linux和Mac OS X系统

打开命令控制台执行如下命令:

$ curl -LsS http://symfony.com/installer > symfony.phar
$ sudo mv symfony.phar /usr/local/bin/symfony
$ chmod a+x /usr/local/bin/symfony

Now you can execute the Symfony Installer as a global system command called symfony.

Windows系统

打开命令控制台执行如下命令:

c:> php -r "readfile('http://symfony.com/installer');" > symfony.phar

Then, move the downloaded symfony.phar file to your projects directory and execute it as follows:

c:> php symfony.phar

创建博客应用

Now that everything is correctly set up, you can create a new project based on Symfony. In your command console, browse to a directory where you have permission to create files and execute the following commands:

# Linux, Mac OS X
$ cd projects/
$ symfony new blog

# Windows
c:> cd projects/
c:projects> php symfony.phar new blog

This command creates a new directory called blog that contains a fresh new project based on the most recent stable Symfony version available. In addition, the installer checks if your system meets the technical requirements to execute Symfony applications. If not, you’ll see the list of changes needed to meet those requirements.

小技巧

Symfony releases are digitally signed for security reasons. If you want to verify the integrity of your Symfony installation, take a look at the public checksums repository and follow these steps to verify the signatures.

代码目录结构

After creating the application, enter the blog/ directory and you’ll see a number of files and directories generated automatically:

blog/
├─ app/
│  ├─ console
│  ├─ cache/
│  ├─ config/
│  ├─ logs/
│  └─ Resources/
├─ src/
│  └─ AppBundle/
├─ vendor/
└─ web/

This file and directory hierarchy is the convention proposed by Symfony to structure your applications. The recommended purpose of each directory is the following:

  • app/cache/, stores all the cache files generated by the application;
  • app/config/, stores all the configuration defined for any environment;
  • app/logs/, stores all the log files generated by the application;
  • app/Resources/, stores all the templates and the translation files for the application;
  • src/AppBundle/, stores the Symfony specific code (controllers and routes), your domain code (e.g. Doctrine classes) and all your business logic;
  • vendor/, this is the directory where Composer installs the application’s dependencies and you should never modify any of its contents;
  • web/, stores all the front controller files and all the web assets, such as stylesheets, JavaScript files and images.

项目bundle

When Symfony 2.0 was released, most developers naturally adopted the symfony 1.x way of dividing applications into logical modules. That’s why many Symfony apps use bundles to divide their code into logical features: UserBundle, ProductBundle, InvoiceBundle, etc.

But a bundle is meant to be something that can be reused as a stand-alone piece of software. If UserBundle cannot be used “as is” in other Symfony apps, then it shouldn’t be its own bundle. Moreover InvoiceBundle depends on ProductBundle, then there’s no advantage to having two separate bundles.

最佳实践

Create only one bundle called AppBundle for your application logic

Implementing a single AppBundle bundle in your projects will make your code more concise and easier to understand. Starting in Symfony 2.6, the official Symfony documentation uses the AppBundle name.

注解

There is no need to prefix the AppBundle with your own vendor (e.g. AcmeAppBundle), because this application bundle is never going to be shared.

All in all, this is the typical directory structure of a Symfony application that follows these best practices:

blog/
├─ app/
│  ├─ console
│  ├─ cache/
│  ├─ config/
│  ├─ logs/
│  └─ Resources/
├─ src/
│  └─ AppBundle/
├─ vendor/
└─ web/
   ├─ app.php
   └─ app_dev.php

小技巧

If your Symfony installation doesn’t come with a pre-generated AppBundle, you can generate it by hand executing this command:

$ php app/console generate:bundle --namespace=AppBundle --dir=src --format=annotation --no-interaction

修改默认的目录结构

If your project or infrastructure requires some changes to the default directory structure of Symfony, you can override the location of the main directories: cache/, logs/ and web/.

In addition, Symfony3 will use a slightly different directory structure when it’s released:

blog-symfony3/
├─ app/
│  ├─ config/
│  └─ Resources/
├─ bin/
│  └─ console
├─ src/
├─ var/
│  ├─ cache/
│  └─ logs/
├─ vendor/
└─ web/

The changes are pretty superficial, but for now, we recommend that you use the Symfony directory structure.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务