Home >Backend Development >PHP Tutorial >Detailed explanation of how symfony uses commands to create projects

Detailed explanation of how symfony uses commands to create projects

*文
*文Original
2018-01-05 14:37:281765browse

How does symfony create a project using commands? This article mainly introduces the method of using the symfony command to create a project, and analyzes the use of the Symfony command and the related skills of project creation in the form of examples. Friends in need can refer to it. I hope to be helpful.

The example in this article describes how to use the symfony command to create a project. Share it with everyone for your reference, as follows:

Overview

This chapter describes the reasonable structural framework of a Symfony project, and uses the symfony command to initialize the project structure.

Introduction

In symfony, a project is a set of services and valid operations under a specified domain name, sharing the same project model.

In a project, the operations in the application are a set of logic; each application can run normally and independently without interfering with other applications in the same project.

In most cases, a project will contain two applications, one responsible for foreground display and one responsible for background processing, using the same database. Of course, you can also include many small sites in one project, each site being a different application. Note that hyperlinks used between different applications must use absolute paths.

Each application is a set of modules, each module is responsible for a special function. A module usually uses a page or a group of pages for similar functionality. For example, modules can be home, articles, help, shoppingCart, account, etc.

Functions of modules: Each module has its own functions. For example, the shoppingCart (shopping cart) module must have add, show and update functions. Functional behavior can be thought of as page behavior in a typical web application.

If a new project has too many levels, it is easy to group all the functions in the module so that the file structure can be kept simple. When the application becomes more complex, functions can be organized in logical modules.

Each application can run in different environments, for example, different configurations or databases. Generally every new application will run in three environments (development, testing and final production). Each application can run in more environments if necessary, and only needs to modify the configuration accessories in different environments.

For example, a test environment will need to log warnings and errors, while a final production environment will only need to log errors. Cache acceleration is usually not turned on in development environments, but needs to be turned on in testing and final production environments. Development and test environments may require test data to be stored in the final product's remote database. All environments can coexist on a single machine, and typically only the final production environment is on the production server.

Note: If you are using symfony through the sandbox, you do not need to set up a project or application. A project named 'sf_sandbox' and a Application named 'frontend'. You don't need to set up a web server either, just place your program in the web/ root directory.

Pake

SymFony uses the specialized tool Pake to manage projects, applications and modules. Pake is a php tool similar to the Rake command (which is a tool that converts the make command to Ruby). It automates some administrative tasks based on a special configuration file called pakefile.php. If you use the pake tool instead of the symfony command line, everything becomes very simple.

To get a list of all valid management operation commands, simply enter in your project directory:

$ symfony -T

CLI (command line operation) task scheduling is used in the early stages of a project stage period. A complete description of CLI task scheduling is available in the CLI chapter.

Project settings

Before everything starts, you must create a new directory to store the project:

$ mkdir /home/steve/myproject

Then, start initializing the project to generate original files and directories , simply enter:

$ cd /home/steve/myproject
$ symfony init-project myproject

This is an overview of the newly created file system tree structure:

apps/
batch/
cache/
config /
data/
doc/
lib/
log/
test/
web/

symfony command can be in the available directory of the current project Call anytime.

Application settings

The project is not complete yet, it still needs at least one application. First use the symfony init-app command to initialize an application, and use the parameters following the command to name the application:

$ symfony init-app myapp

This creates a myapp directory in the apps/ folder in the root directory of the project. It contains a default application configuration and a set of directory files for your site:

apps/
myapp/
config/
i18n/
lib/
modules/
templates/

Some php files that act as front-end controllers in their respective default environments are also created in the web directory of the project root directory:

web/
index.php
myapp_dev.php

index.php是production新应用程序的前端控制器。因为你创建这个项目中的第一个应用程序时,Symfony创建了一个调用 index.php的文件,例如 myapp.php (如果你现在添加一个名为 mynewapp 的新应用程序,新产品的前端控制器将被命名为mynewapp.php)。在 开发环境中运行程序时,调用前端控制器 myapp_dev.php。

注意:你如果仔细阅读了介绍,你可能会对myapp_test.php文件的位置感到意外。事实上,测试 环境是用于对你的应用程序的构件进行单元测试,它不需要前端控制器。可以参考单元测试章节去了解更多详细内容。

从现在开始,/home/steve/myproject/ 目录将会作为项目的根目录。根目录的路径已经被保存为 SF_ROOT_DIR 常量,定义在 index.php 文件中,并且我们将会用这个常量名代替实际路径以避免把不叫Steve的读者搞糊涂了(译者注:因为作者将项目放在/home/steve /myprojetc的目录下,这个路径每个人可能都不尽相同,所以使用常量SF_ROOT_DIR来代替了实际路径)。

Web服务器设置

为了访问和测试新的应用程序,需要配置web服务器。这有一个Apache的例子,在 httpd.conf 配置文件中加入一个新的虚拟主机:

<Directory "/$data_dir/symfony/web/sf">
 AllowOverride All
 Allow from All
</Directory>
<VirtualHost *:80>
 ServerName myapp.example.com
 DocumentRoot "/home/steve/myproject/web"
 DirectoryIndex index.php
 Alias /sf /$data_dir/symfony/web/sf
 <Directory "/home/steve/myproject/web">
  AllowOverride All
  Allow from All
 </Directory>
</VirtualHost>

注意:上面的配置中的 $data_dir 变量需要替换成你的PEAR库目录。例如:在*nix系统中,你可以输入:

<code> Alias /sf /usr/local/lib/php/data/symfony/web/sf</code>

重启Apache服务之后,就可以看到调用新创建的应用程序的页面,只需要在一个标准的web浏览器的地址栏输入下列路径:

http://myapp.example.com/index.php/

或者,在调试模式下使用这个路径:

http://myapp.example.com/myapp_dev.php/

注意:Symfony显示‘简短漂亮的(smart)'路径时用到了 mod_rewrite 模块。如果你的Apache版本没有将 mod_rewrite 模块编译进去,那么需要在 httpd.conf 中检查模块mod_rewrite是否是动态模块方式(DSO)安装的,并且确认是否已经打开(译者注:关于Apache的mod_rewrite模块安 装和使用方法请参考Apache相关文档,这里假设读者已经具备这方面知识而不作过多说明):

AddModule mod_rewrite.c
LoadModule rewrite_module modules/mod_rewrite.so

模块设置

你这个新的应用程序并不出众,它缺乏吸引人的功能。如果你想增加功能性,你需要在在其中用到模块。这里再一次用到了 symfony 命令,参数为 init-module ,后面跟着应用程序名称和新模块的名称:

$ symfony init-module myapp mymodule

创建以后的树结构如下:

modules/
  mymodule/
    actions/
    config/
    lib/
    templates/
    validate/

新模块直接可以被使用:

http://myapp.example.com/index.php/mymodule

然后你需要让它正常的工作,编辑文件 myapp/modules/mymodule/templates/indexSuccess.php 输入:

Hello, world !

保存它,刷新刚才的页面就可以看到内容!

源文件版本控制(Source versioning)

应用程序设置完成之后,建议开始进行源文件版本控制。Symfony从一开始就支持CVS(译者注:版本控制系统),建议使用Subversion(译者注:一个版本控制系统软件,采用CVS 的运作模型, 并以取代CVS 为目标)。下面的例子列出了一些Subversion的命令,用于从在一个安装了Subversion的服务器上创建一个新项目的"仓库"(译者注:repository,源代码储存的地方)。对于Windows用户,建议客户端使用TortoiseSVN。关于源文件版本控制的更多信息和命令用法,请参考Subversion文档。

下面的例子假设$SVNREP_DIR是一个已经定义的环境变量。如果你还没有定义它,你需要用"仓库"的实际路径代替$SVNREP_DIR变量。

现在让我们开始创建myproject项目的新"仓库":

$ svnadmin create $SVNREP_DIR/myproject

然后用下面这串命令创建新"仓库"的基本组织结构(规划),其中包含 trunk, tags 和 branches 三个目录:

[code]$ svn mkdir -m "layout creation" file:///$SVNREP_DIR/myproject/trunk file:///$SVNREP_DIR/myproject/tags file:///$SVNREP_DIR/myproject/branches[/code]

这将是你第一个版本。现在你必须导入项目的文件,但不包括缓存和日志等临时文件:

$ cd /home/steve/myproject
$ rm -rf cache/*
$ rm -rf log/*
$ svn import -m "initial import" . file:///$SVNREP_DIR/myproject/trunk

检查提交的文件:

$ svn ls file:///$SVNREP_DIR/myproject/trunk/

看上去很不错。现在SVN"仓库"已经记录了所有项目文件的版本(和更改历史)。就是说实际路径为 /home/steve/myproject 的目录中所有的文件都已经被"仓库"记录。要做到这一点,首先重命名 myproject 目录名,当一切运行正常的时候可以删除它,并且在新目录中向"仓库"提交一个checkout:

$ cd /home/steve
$ mv myproject myproject.origin
$ svn co file:///$SVNREP_DIR/myproject/trunk myproject
$ ls myproject

现在你可以在 /home/steve/myproject/ 目录下的文件中工作,并且提交修改到"仓库"中。不要忘记作一些清理和删除myproject.origin目录,它现在没有用了。

还有一些另外的设置。当你向"仓库"中提交工作目录时,会复制一些多余的文件,像项目中 cache 和 log 目录下的文件。因此你需要针对这个项目在svn中指定一个忽略列表。你也需要重新将 cache/ 和 log/ 目录的权限设置为完全控制,在访问时产生的文件SVN将不会储存:

$ cd /home/steve/myproject
$ svn propedit svn:ignore .
$ chmod 777 cache
$ chmod 777 log

这将调用在SVN中设置的默认的文本编辑器。如果没有生效,就像下面这样设置subversion首选的编辑器:

$ export SVN_EDITOR=<name of editor>
$ svn propedit svn:ignore .

直接在SVN中的忽略列表中加入myproject子目录,这样提交的时候就忽略了:

cache
log

保存然后退出,这样就完成了。

相关推荐:

简述Symfony核心类

symfony window下安装时候出现问题的解决方法

symfony安装详细教程

The above is the detailed content of Detailed explanation of how symfony uses commands to create projects. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn