版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、The Cookbookfor Symfony 2.3generated on July 14, 2013The Cookbook (2.3)This work is licensed under the “Attribution-Share Alike 3.0 Unported” license (licenses/by-sa/3.0/).You areto share (to copy, distribute and transmit the work), and to remix (to adapt the work) under thefollowing conditions: Att
2、ribution: You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). Share Alike: If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar
3、or a compatible license. For any reuse or distribution, you must make clear to others the license terms of this work.The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor
4、SensioLabs shall have any liability toanyor entity with respect to any loss or damage caused or alleged to be caused directly or indirectly bythe information contained in this work.If you find typos or errors, feel (continuously updated.to report them by creating a ticket on the Symfony ticketing sy
5、stem). Based on tickets and users feedback, this book isContents at a GlanceHow to Create and store a Symfony2 Project in git5How to Create and store a Symfony2 Project in Subversion9How to customize Error Pages13How to define Controllers as Services15How to force routes to always use HTTPS or HTTP1
6、9How to allow a "/" character in a route parameter20How to configure a redirect to another route without a custom controller21How to use HTTP Methods beyond GET and POST in Routes22How to use Service Container Parameters in your Routes24How to create a custom Route Loader25How to Use Asset
7、ic for Asset Management29How to Minify CSS/JS Files (using UglifyJs and UglifyCss)34How to Minify JavaScripts and Stylesheets with YUI Compressor38How to Use Assetic For Image Optimization with Twig Functions40How to Apply an Assetic Filter to a Specific File Extension43How to handle File Uploads wi
8、th Doctrine45How to use Doctrine Extensions: Timestampable, Sluggable, Translatable, etc.54How to Register Event Listeners and Subscribers55How to use Doctrine's DBAL Layer58How to generate Entities from an Existing Database60How to work with Multiple Entity Managers and Connections64How to Regi
9、ster Custom DQL Functions67How to Define Relationships with Abstract Classes and Interfaces68How to provide mclasses for several Doctrine implementations71How to implement a simple Registration Form74How to customize Form Rendering80How to use Data Transformers91How to Dynamically Modify Forms Using
10、 Form Events97How to Embed a Collection of Forms107How to Create a Custom Form Field Type120How to Create a Form Type Extension125How to Reduce Code Duplication with "inherit_data"130How to Unit Test your Forms133How to configure Empty Data for a Form Class138How to use the submit() Functi
11、on to handle Form Submissions140How to create a Custom Validation Constraint143PDF brought to you by generated on July 14, 2013Contents at a Glance | iiiHow to Master and Create new Environments146How to override Symfony's Defauirectory Structure151Understanding how the Front Controller, Kernel
12、and Environments work together154How to Set External Parameters in the Service Container157How to use PdoSessionHandler to store Sessions in the Database160How to use the Apache Router163Configuring a web server165How to use the Serializer167How to create an Event Listener169How to work with Scopes1
13、72How to work with Compiler Passes in Bundles177Bridge a legacy application with Symfony Sessions178Session Proxy Examples179Making the Locale "Sticky" during a User's Session182Configuring the Directory where Sessions Files are Saved184How to create a custom Data Collector185How to Cr
14、eate a SOAP Web Service in a Symfony2 Controller188How Symfony2 differs from symfony1192How to deploy a Symfony2 application197Contents at a Glance | 4iv | Contents at a GlanceChapter 1How to Create and store a Symfony2 Project in gitThough this entry is specifically about git, the same generic prin
15、ciples will apply if you're storing your project in Subversion.Once you'vethrough Creating Pages in Symfony2 and become familiar with using Symfony, you'llno-doubt bey to start your own project. In this cookbook article, you'll learn the best way to start anew Symfony2 project that
16、39;s stored using the git1 source control management system.Initial Project SetupTo get started, you'll need to download Symfony and initialize your local git repository:1. Download the Symfony2 Standard Edition2 without vendors.2. Unzip/untar the distribution. It will create a folder called Sym
17、fony with your new project structure, config files, etc. Rename it to whatever you like.3. Create a new file called .gitignore at the root of your new project (e.g. next to the composer.json file) and paste the following into it. Files matching these patterns will be ignored by git:123456/web/bundle
18、s/app/bootstrap*/app/cache/*/app/logs/*/vendor/app/config/parameters.ymlListing 1-11.2.PDF brought to you by generated on July 14, 2013Chapter 1: How to Create and store a Symfony2 Project in git | 5You may also want to create a .gitignore file that can be used system-wide, in which case, you can.gi
19、tignore3 This way you can exclude files/folders often used byfind more information here: your IDE for all of your projects.4.Copytoapp/config/parameters.yml.dist.Theapp/config/parameters.ymlparameters.yml file is ignored by git (see above) so that machine-specific settings like databasepasswords are
20、n't committed. By creating the parameters.yml.dist file, new developers canquickly clone the project, copy this file to parameters.yml, customize it, and start develoInitialize your git repository:.5.1 $ git initListing 1-26.Add all of the initial files to git:Listing 1-31 $ git add .7.Create an
21、 initial commit with your started project:1 $ git commit -m "Initial commit"Listing 1-48.Finally, download all of the third-party vendor libraries by executing composer. ForUpdating Vendors.s, seeAt this point, you have a fully-functional Symfony2 project that's correctly committed to
22、git. You can immediately begin development, committing the new changes to your git repository.You can continue to follow along with the Creating Pages in Symfony2 chapter to learn more about how to configure and develop inside your application.The Symfony2 Standard Edition comes with some example fu
23、nctionality. To remove the sample code, follow the instructions in the "How to remove the AcmeDemoBundle" article.Managing Vendor Libraries with composer.jsonHow does it work?Every Symfony project uses a group of third-party "vendor" libraries. One way or another the goal is to d
24、ownload these files into your vendor/ directory and, ideally, to give you some sane way to manage the exact version you need for each.By default, these libraries are downloaded by running a php composer.phar install "downloader"binary. This composer.phar file is from a library called Compo
25、ser4 and you can installing it in the Installation chapter.more aboutThe composer.phar files from the composer.json file at the root of your project. This is an JSON-formatted file, which holds a list of each of the external packages you need, the version to be downloadedand more. The composer.phar
26、file alsos from a composer.lock file, which allows you to pin eachlibrary to an exact version. In fact, if a composer.lock file exists, the versions inside will override thosein composer.json. To upgrade your libraries to new versions, run php composer.phar update.3.4.PDF brought to you by generated
27、 on July 14, 2013Chapter 1: How to Create and store a Symfony2 Project in git | 6If you want to add a new package to your application, modify the composer.json file:Listing 1-5"require": ."doctrine/doctrine-fixtures-bundle": "dev"and then execute the updated for this sp
28、ecific package, i.e.:Listing 1-61 $ php composer.phar update doctrine/doctrine-fixtures-bundleYou can also combine both steps into a singled:1 $ php composer.phar require doctrine/doctrine-fixtures-bundle:devListing 1-7To learn more about Composer, see GetC5:It's important to realize
29、that these vendor libraries are not actually part of your repository. Instead, they're simply un-tracked files that are downloaded into the vendor/. But since all the information needed to download these files is saved in composer.json and composer.lock (which are stored in the repository), any
30、other developer can use the project, run php composer.phar install, and download the exact same set of vendor libraries. This means that you're controlling exactly what each vendor library looks like, without needing to actually commit them to your repository.So, whenever a developer uses your p
31、roject, he/she should run the php composer.phar install script to ensure that all of the needed vendor libraries are downloaded.Upgrading SymfonySince Symfony is just a group of third-party libraries and third-party libraries are entirely controlled through composer.json and composer.lock, upgrading
32、 Symfony means simply upgrading each of these files to match their state in the latest Symfony Standard Edition.Of course, if you've added new entries to composer.json, be sure to replace only the original parts(i.e. be sure not to alsete any of your custom entries).Vendors and SubmodulesInstead
33、 of using the composer.json system for managing your vendor libraries, you may instead choose to use native git submodules6. There is nothing wrong with this approach, though the composer.json system is the official way to solve this problem and probably much easier to deal with. Unlike git submodul
34、es, Composer is smart enough to calculate which libraries depend on which other libraries.Storing your Project on a Remote ServerYou now have a fully-functional Symfony2 project stored in git. However, in most cases, you'll also want to store your project on a remote server both for backup purpo
35、ses, and so that other developers can collaborate on the project.5.6.PDF brought to you by generated on July 14, 2013Chapter 1: How to Create and store a Symfony2 Project in git | 77.The easiest way to store your project on a remote server is via however you will need to pay a monthly fee to host pr
36、ivate repositories.Public repositories are,Alternatively, you can store your git repository on any server by creating a barebones repository8 and then pushing to it. One library that helps manage this is Gitolite.PDF brought to you by generated on July 14, 2013Chapter 1: How to Create and sto
37、re a Symfony2 Project in git | 8Chapter 2How to Create and store a Symfony2 Project in SubversionThis entry is specifically about Subversion, and based on principles found in How to Create and store a Symfony2 Project in git.Once you'vethrough Creating Pages in Symfony2 and become familiar with
38、using Symfony, you'llno-doubt bey to start your own project. The preferred method to manage Symfony2 projects is usinggit1 but some prefer to use Subversion2 which is totally fine!. In this cookbook article, you'll learn how to manage your project using svn3 in a similar manner you would do
39、with git4.This is a method to tracking your Symfony2 project in a Subversion repository. There are several ways to do and this one is simply one that works.The Subversion RepositoryFor this article it's assumed that your repository layout follows the widespstandard structure:1234myproject/branch
40、es/ tags/ trunk/Listing 2-.PDF brought to you by generated on July 14, 2013Chapter 2: How to Create and store a Symfony2 Project in Subversion | 9Most subversion hosting should follow this standard practice. This is the recommended layout in Version Control with Subversion5 and the layout us
41、ed by most hosting (see Subversion hosting solutions).Initial Project SetupTo get started, you'll need to download Symfony2 and get the basic Subversion setup:Download the Symfony2 Standard Edition6 with or without vendors.Unzip/untar the distribution. It will create a folder called Symfony with
42、 your new project structure, config files, etc. Rename it to whatever you like.Checkout the Subversion repository that will host this project. Let's say it is hosted on Google code7 and called myproject: $ svn checkoutmyprojectListing 2-24.Copy the Symfony2 project files in the subversion
43、 folder:1 $ mv Symfony/* myproject/Listing 2-35.Let's now set the ignore rules. Not everything should be stored in your subversion repository. Some files (like the cache) are generated and others (like the database configuration) are meant to be customized on each machine. This makes use of the
44、svn:ignore property, so that specific files can be ignored.123456789101112$ cd myproject/$ svn add -depth=empty app app/cache app/logs app/config webListing 2-4$ svn propset svn:ignore "vendor" .$ svn propset svn:ignore "bootstrap*" app/$ svn propset svn:ignore "parameters.y
45、ml" app/config/$ svn propset svn:ignore "*" app/cache/$ svn propset svn:ignore "*" app/logs/$ svn propset svn:ignore "bundles" web$ svn ci -m "commit basic Symfony ignore list (vendor, app/bootstrap*, app/config/ parameters.yml, app/cache/*, app/logs/*, web/bu
46、ndles)"6.The rest of the files can now be added and committed to the project:1 $ svn add -force .2 $ svn ci -m "add basic Symfony Standard 2.X.Y"Listing 2-57.Copyapp/config/parameters.ymltoapp/config/parameters.yml.dist.Theparameters.yml file is ignored by svn (see above) so that mach
47、ine-specific settings like databasepasswords aren't committed. By creating the parameters.yml.dist file, new developers canquickly clone the project, copy this file to parameters.yml, customize it, and start develo.5.6.7.PDF brought to you by generated on July 14, 2013Chapter 2: How to Create an
48、d store a Symfony2 Project in Subversion | 108. Finally, download all of the third-party vendor libraries by executing composer. ForUpdating Vendors.s, seeIf you rely on any "dev" versions, then git may be used to install those libraries, since there is no archive available for download.At
49、 this point, you have a fully-functional Symfony2 project stored in your Subversion repository. The development can start with commits in the Subversion repository.You can continue to follow along with the Creating Pages in Symfony2 chapter to learn more about how to configure and develop inside you
50、r application.The Symfony2 Standard Edition comes with some example functionality. To remove the sample code, follow the instructions in the "How to remove the AcmeDemoBundle" article.Managing Vendor Libraries with composer.jsonHow does it work?Every Symfony project uses a group of third-p
51、arty "vendor" libraries. One way or another the goal is to download these files into your vendor/ directory and, ideally, to give you some sane way to manage the exact version you need for each.By default, these libraries are downloaded by running a php composer.phar install "download
52、er"binary. This composer.phar file is from a library called Composer8 and you can installing it in the Installation chapter.more aboutThe composer.phar files from the composer.json file at the root of your project. This is an JSON-formatted file, which holds a list of each of the external packa
53、ges you need, the version to be downloadedand more. The composer.phar file alsos from a composer.lock file, which allows you to pin eachlibrary to an exact version. In fact, if a composer.lock file exists, the versions inside will override thosein composer.json. To upgrade your libraries to new vers
54、ions, run php composer.phar update.If you want to add a new package to your application, modify the composer.json file:Listing 2-6"require": ."doctrine/doctrine-fixtures-bundle": "dev"and then execute the updated for this specific package, i.e.:Listing 2-71 $ php compos
55、er.phar update doctrine/doctrine-fixtures-bundleYou can also combine both steps into a singled:1 $ php composer.phar require doctrine/doctrine-fixtures-bundle:devListing 2-88.PDF brought to you by generated on July 14, 2013Chapter 2: How to Create and store a Symfony2 Project in Subversion | 11To le
56、arn more about Composer, see GetC9:It's important to realize that these vendor libraries are not actually part of your repository. Instead, they're simply un-tracked files that are downloaded into the vendor/. But since all the information needed to download these files is saved i
57、n composer.json and composer.lock (which are stored in the repository), any other developer can use the project, run php composer.phar install, and download the exact same set of vendor libraries. This means that you're controlling exactly what each vendor library looks like, without needing to
58、actually commit them to your repository.So, whenever a developer uses your project, he/she should run the php composer.phar install script to ensure that all of the needed vendor libraries are downloaded.Upgrading SymfonySince Symfony is just a group of third-party libraries and third-party libraries are entirely controlled through composer.json and composer.lock, upgrading Symfony means simply upgrading each of these files to match their
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年企业员工竞业禁止协议
- 人教版八年级下册12.3机械效率教案
- 2023-2024学年二年级下学期数学第六单元第四课时《三位数的加法和减法》(学案)
- 二年级上册数学教案-5.1观察物体|人教新课标版
- 人教部编版五年级上册语文《第五单元习作例文》教学设计及教学反思
- 中班美术活动教案及教学反思《爱心花朵》
- 交通运输废旧物资回收方案
- 中班主题详案教案:生活中的筷子
- MDT管理制度在药物研发中的应用
- 2024年企业间关于生物基因技术研发与转让合同
- 模拟联合国大会流程及培训指导
- 临检中心检验科主任会议 6.1质量、速度与安全:POCT网络化管理
- 牙龈病PPT课件可编辑
- SB/T 11223-2018管理培训服务规范
- 体育选项项目内容及考核要求(5)网 球
- FZ/T 64041-2014熔喷纤网非织造粘合衬
- 高品质变压器外观品质检验基础
- 革命烈士杨靖宇主要事迹分析
- 《网络设备安装与调试(华为eNSP模拟器)》项目1认识eNSP模拟器及VRP基础操作
- 《简单的周期问题》教学设计和说课稿
- 实验室生物安全管理体系结构框架图
评论
0/150
提交评论