How to create a project by using the Symfony command _php instance

Source: Internet
Author: User
Tags chmod documentation mkdir pear php programming svn version control system

The example in this article describes how to create a project using the Symfony command. Share to everyone for your reference, specific as follows:

Overview

This section describes a reasonable structural framework for a Symfony project and uses the Symfony command to initialize the project structure.

Introduced

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

In a project, the operation in the application is a set of logic; Each application can run independently of the other applications in the same project.

In most cases, one project will contain two applications, one for the foreground display, one for background processing, and the same database for use. Of course, you can also include a lot of small sites in a project, each site is a different application. Note hyperlinks used between different applications must use an absolute path.

Each application is a set of modules, each of which is responsible for a particular function. A module usually uses a page or a set of pages for similar functions. For example, the module can be home, articles, help, ShoppingCart, account, and so on.

Module functions: Each module has its own functions, such as the ShoppingCart (shopping cart) module to have Add (add), display (show) and Updates (update) features. The behavior of a feature can be viewed as a page behavior in a typical Web application.

If a new project has too many levels, it is easy to group all the features in the module so that the file structure can be kept simple. When the application is more complex, you can organize the function in the logic module.

Each application can run in a different environment, such as a different configuration or database. In general, each new application will run in three environments (development, testing, and final products). Each application can run in more environments if needed, and only need to modify the configuration accessories in different environments.

For example, a test environment needs to record warnings and errors, and a final product environment will only need to log errors. Cache acceleration is typically not turned on in the development environment, and is required in test and final product environments. The development environment and test environment may require data to be tested and stored in the remote database of the final product. All environments can coexist on a single machine, and the product server is usually the final product environment.

Note: If you are using symfony through a sandbox (sandbox), you do not need to set up a project or application, the sandbox (sandbox) has been prepared with a project named ' Sf_sandbox ' and an application named ' Frontend '. You don't need to set up a Web server, just place your program in the web/root directory.

Pake

Symfony uses specialized tools Pake to manage projects, applications, and modules. Pake is a PHP tool, similar to the Rake command (this is a tool that converts make commands 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, all of the operations will become very simple.

To get a list of all the effective management action commands, simply enter them in your project directory:

$ symfony-t

The CLI (command-line operation) Task Scheduler is used during the prophase phase of a project. A complete description of the CLI task scheduling reference CLI chapter.

Project Settings

Before you start, you have to create a new directory for your project:

$ mkdir/home/steve/myproject

Then, start initializing the project to generate the original files and directories, simple input:

$ 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/

The Symfony command can be invoked at any time in the available directory of the current project.

Apply Settings

The project has not been completed yet, and it needs at least one application. First use the Symfony Init-app command to initialize an application, using the parameters followed by the command to name the application:

$ symfony Init-app MyApp

This creates a MyApp directory in the apps/folder at the root of the project, which contains a default application configuration and a set of catalog 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's root directory:

web/
index.php
myapp_dev.php

Index.php is the front-end controller for production new applications. Since you created the first application in this project, Symfony created a file called index.php, such as myapp.php (if you now add a new application called Mynewapp, The front-end controller for the new product will be named mynewapp.php). When running programs in the development environment, call the front-end controller myapp_dev.php.

Note: If you read the introduction carefully, you may be surprised at the location of the myapp_test.php file. In fact, the test environment is used to unit test the artifacts of your application, and it does not require a front-end controller. You can refer to the Unit Test section for more detailed information.

From now on, the/home/steve/myproject/directory will be the root directory for the project. The path to the root directory has been saved as a Sf_root_dir constant, defined in the index.php file, and we will replace the actual path with this constant name to avoid confusing readers who don't call Steve: Because the author puts the project in the/home/steve/ Myprojetc directory, this path may be different for everyone, so use constant sf_root_dir instead of the actual path.

Web server Settings

To access and test new applications, you need to configure the Web server. Here's an example of Apache that adds a new virtual host to the httpd.conf configuration file:

<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>

Note: The $data _dir variable in the above configuration needs to be replaced with your Pear library directory. For example: In the *nix system, you can enter:

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

You can find more information about the Pear directory in the installation section.

After restarting the Apache service, you can see the page that invokes the newly created application by entering the following path in the address bar of a standard Web browser:

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

Alternatively, use this path in debug mode:

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

Note: The Mod_rewrite module is used when Symfony displays a ' short beautiful ' (smart) path. If your Apache version does not compile the Mod_rewrite module, then you need to check that the module mod_rewrite is installed in the Dynamic module mode (DSO) in httpd.conf and confirm that it has been opened (translator: About Apache Mod_ Rewrite module installation and usage refer to the Apache documentation, which assumes that the reader already has this knowledge without too much explanation:

Addmodule mod_rewrite.c
LoadModule rewrite_module modules/mod_rewrite.so

You can learn more about short paths (smart URLs) in the Routing (routing) section.

Symfony is compatible with other server configuration methods. You can also, for example, use aliases (alias) to access Symfony applications instead of virtual hosts. For more information about Web server configuration, see the relevant chapters.

Module settings

Your new application is not outstanding, it lacks the appealing function. If you want to add functionality, you need to use modules in it. Once again, the Symfony command is used, with the argument init-module, followed by the application name and the name of the new module:

$ symfony init-module MyApp mymodule

The next tree structure is created as follows:

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

The new module can be used directly:

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

Then you need to make it work properly, edit the file myapp/modules/mymodule/templates/indexsuccess.php input:

Hello, World!

Save it, refresh the page just to see the content!

Source file version control (sources versioning)

After the application settings are complete, it is recommended that you start source file versioning. Symfony from the outset to support CVS (the Translator Note: Version control system), the proposed use of Subversion (translator Note: A version control system software, using a CVS operation model, and to replace CVS as the goal). The following example lists some of the Subversion commands used to create a "warehouse" of a new project on a server that has Subversion installed (translator: Repository, where source code is stored). For Windows users, it is recommended that clients use TORTOISESVN. For more information and command usage for source file versioning, refer to the Subversion documentation.

The following example assumes that $svnrep_dir is an environment variable that has already been defined. If you haven't defined it yet, you need to replace the $svnrep_dir variable with the actual path of the "warehouse".

Now let's start by creating a new "warehouse" for the MyProject project:

$ svnadmin Create $SVNREP _dir/myproject

Then use the following command to create a new "warehouse" of the basic organizational structure (planning), which contains trunk, tags and branches three directories:

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

This will be your first version. You must now import the project's files, but not include temporary files such as caching and logging:

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

Check the submitted file:

$ svn ls file:///$SVNREP _dir/myproject/trunk/

It looks good. Now SVN "Warehouse" has recorded the version (and change history) of all project files. This means that all files in the directory where the actual path is/home/steve/myproject are already logged in the warehouse. To do this, first rename the MyProject directory name, and when everything is working, you can delete it and submit a checkout to "warehouse" in the new directory:

$ cd/home/steve
$ mv myproject myproject.origin
$ svn co file:///$SVNREP _dir/myproject/trunk myproject
$ ls MyProject

Now you can work in the files in the/home/steve/myproject/directory and commit the changes to the warehouse. Don't forget to make some cleanup and delete the Myproject.origin directory, it's no use now.

There are also a few other settings. When you submit a working directory to the "warehouse", you will copy some of the extra files, such as files in the cache and log directories in the project. So you need to specify an ignore list in SVN for this project. You also need to reset the permissions for the cache/and log/directories to full control, and the files generated at the time of the visit will not be stored by SVN:

$ cd/home/steve/myproject
$ svn propedit svn:ignore.
$ chmod 777 Cache
$ chmod 777 Log

This will invoke the default text editor set in SVN. If not, set the Subversion preferred editor as follows:

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

Add the MyProject subdirectory directly to the Ignore list in SVN, so the submission ignores:

Cache
Log

Save and then exit, so it's done.

I hope this article will help you with your PHP programming based on the Symfony framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.