中英文對照 介紹Play Framework 框架 模塊

使用Play的模塊

h1. Using Play modules

 

一個Play的應用可以是一些應用模塊的集合,這允許你可以在不同的應用中重用應用的組件,或在一個大的項目中將應用分割爲幾個小的應用。

A Play application can be assembled from several application modules. This allows you to reuse application components across several application or split a large application into several smaller applications.

 

什麼是模塊

h2. <a>What is a module?</a>

 

一個模塊只是另一個Play應用,只是在應用的模塊中的資源的載入方面中有一些不同。

A module is just another Play application; however some differences exist in the way resources are loaded for an application module:

 

一個模塊不能有conf/application.conf文件

一個模塊可以有conf/routes文件,但是這些路由不會被自動載入。

首先在主應用的路徑中搜索所有文件,然後在所有載入的模塊總搜索文件。

一個模塊可以在module/lib中包含java字節碼包(jar包)

模塊中的所有東西都是可選的。

* A module does not have a **conf/application.conf** file.

* A module can have a **conf/routes** file, but these routes will not be  loaded automatically.

* All files are first searched for in the main application path, then in all loaded modules.

* A module can contain plain Java code packaged as a JAR file in the module/lib directory.

* Everything in a module is optional.

 

你可以使用play new-module命令創建一個模塊。

You can create a module with the **play new-module** command.

 

怎樣從一個應用中載入一個模塊?

h2. <a>How to load a module from an application</a>

 

默認的模塊的安裝路徑在$PLAY_HOME/modules文件夾中。這個路徑下的模塊會自動的對使用這個框架的應用可用。

The default modules installation path is in the **$PLAY_HOME/modules** directory. This way all modules are automatically available to all applications run with the framework.

 

如果你需要從一個應用中載入一個額外的模塊,只需要在住應用的application.conf文件中聲明額外的模塊,如下:

If you want to load external modules from an application, just declare external modules in the application.conf file in the main application.

 

 

bc. # Additional modules

# ~~~~~

# A module is another Play application. Add a line for each module you want

# to add to your application. Module paths are either absolute or relative to

# the application root.

#

module.crud=${play.path}/modules/crud

 

你還可以創建一個獨立的項目包含你的應用中所需的所有模塊,你只需要把所有的模塊複製到你的應用中相同的位置即可。

You can also create a standalone project that contains all modules needed by your application. You just need to copy all modules into the same directory as your application. 

 

例如你有一個大的項目,包含一個CMS組件,一個論壇組件,一個號碼簿組件,而且使用了GWT模塊,你可以建立一個項目使用下面的這個佈局。

Let’s say you have a large application with a CMS component, a forum component, a directory component and using the GWT module. You can create a project using this layout:

 

bc. /my-project

    /cms

    /forum

    /directory

    /gwt

    /main

 

在這裏main是你的主項目所在的文件夾(使用play new 命令創建的),gwt是一個從模塊庫中安裝的模塊,cms,forum,director是使用(play new-module命令創建的)。

Here **main** is the directory of your main application (created using the **play new** command), **gwt** is a module installed from the modules repository, and **cms**, **forum** and **directory** are modules created using the **play new-module** command.

 

現在我們在主項目的配置文件中添加這些導入的模塊。

Now from the main application configuration file (my-project/main/conf/application.conf), you can load these modules using:

 

bc. # Additional modules

# ~~~~~

mdoule.gwt=../gwt

module.cms=../cms

module.forum=../forum

module.directory=../directory

 

當你運行主項目時(使用 play run my-project/main),它會把所有這些模塊加載到內存之內存中作爲一個大的項目。

When you run the main application (using **play run my-project/main**) it will load all these modules in memory as a larger application.

 

如果模塊的路徑是相對的,那麼它們相對的是主應用的根目錄。

p(note). If module paths are relative, they are resolved from the main application root.

 

從模塊中載入默認的路由。

h2. <a>Load default routes from modules</a>

 

一個模塊可以提供一個默認的路由文件,你可以把它導入到主應用的路由文件中,使用一個特殊的路由聲明。

A module can provide a default routes file. You can load it in the main application routes file, using a special route declaration:

 

bc. # Import the default CRUD routes

GET     /admin     module:crud

 

你甚至可以從所有可得到的模塊中導入所有的路由。

You can even load routes from all available modules:

 

bc. GET     /     module:*

 

使用模塊庫

h2. <a>Using the module repository</a>

 

模塊倉庫認定了所有社區貢獻的模塊,一個模塊可以有很多版本,你需要檢查模塊的文檔去知道你需要的是哪個版本。

The "module repository":http://www.playframework.org/modules identifies all modules contributed by the community. A module can have several versions. You have to check the module’s documentation for which version you need to use for your framework version.

 

你還可以查看模塊庫中所有的模塊,使用play list-modules命令。

You can also browse the module repository using the **play list-modules** command. 

 

bc. gbo-mac:~ guillaume$ play11 list-modules

~        _            _ 

~  _ __ | | __ _ _  _| |

~ | '_ \| |/ _' | || |_|

~ |  __/|_|\____|\__ (_)

~ |_|            |__/   

~

~ play! 1.1-unstable-r761, http://www.playframework.org

~

~ You can also browse this list online at http://www.playframework.org/modules

~

~ [bespin]

~   Bespin online editor

~   http://www.playframework.org/modules/bespin

~   Versions: 1.0, 1.0.1

~

~ [cobertura]

~   Cobertura

~   http://www.playframework.org/modules/cobertura

~   Versions: 1.0

...

 

你可以是使用命令play install {module}-{version}安裝模塊,例如,爲框架安裝Scala支持,使用命令:play install scala-head。

You can install a module using the **play install {module}-{version}** command. For example, to install the Scala support to the framework, use:

 

bc. play install scala-head

 

按慣例,使用head版本號的版本是模塊的不穩定版本。你也可以忽略版本號,直接安裝默認的模塊的版本。例如play install scala。

By convention the **head** version is the unstable version of the module. You can also install the default version of a module by omitting the version information. For example:

 

bc. play install scala

 

按這種方式安裝的模塊被下載到了你的框架的安裝目錄下的/modules文件夾中。你可以使用--path選項改變安裝位置。

Modules installed this way are downloaded to the **/modules** directory of your framework installation. You can change the installation path using the **--path** option:

 

bc. play install gwt --path=my-project

 

爲模塊倉庫貢獻新的模塊

h2. <a>Contributing a new module to the module repository</a>

 

首先你需要一個OpenbID。它幫助我們確認你是你的模塊的作者。然後在Google Group中發給我們一個模塊登記請求。

First you need to have an OpenID. It will help us to authenticate you as author of your modules. Then send us a module registration request on the Google Group.

 

請告訴我們:

 更多的關於你的模塊的信息。

 你的模塊的名稱,他必須匹配[a-zA-Z]正則表達式

 模塊的簡短描述

 你的項目主頁

 你的OpenID

 你的模塊必須被託管到某個地方,以便使代碼可得到並且可以提交bugs,如果你沒有主意,那麼github,Google Code和Launchpad都是不錯的選擇。

Please tell us:

 

* More about your module. What is it?

* Your module name. It must match the [a-zA-Z]+ regular expression.

* A short description of the module.

* Your project home page.

* Your OpenID.

* Your module must be hosted somewhere with the source code available and a way to report bugs. If you don’t have any idea, github, Google Code and Launchpad are good choices. 

 

需要發行你的模塊,簡單的使用play build-module就可以了,然後連接模塊庫中上傳生成的包。

To release your module, simply use the play build-module command. Then connect to the module repository and upload the generated package.

 

你當然還可以使用官方的Google  Group去尋求幫助,或者分享關於你的工作的信息。

You can of course use the offical Google Group to provide help and share information about your work.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章