symfony簡介
DESCRIPTION
phpconf 2011 Symfony簡介 簡報檔TRANSCRIPT
Symfony 簡介
Who am I
Ricky 是我 http://ricky.ez2.us/ [email protected] http://www.plurk.com/rickysu
Symfony
a web application framework for PHP projects
Version 1.0
released early 2007under MIT license.
sponsored by Sensio
Symfony 整合了這些東西
Mojavi3DEV Controller Propel , Doctrine ORM Prado for i18n support Spyc for YAML Parsing Pake for commandline tool
Symfony 也參考了其他 Framework
Ruby Rails : Helpers for templates, routing.
Java Spring : Dependency injection container.
Python Django : request / response , controller
Symfony 也創造了一些新東西
Web debug toolbar 。 新的 Form 元件,取代 helper 系統。
但這都不是重點
如何使用 Symfony !
開發工具 ?
Netbeans Demo
The Routing
# apps/frontend/config/routing.ymlmember:
url: /:module.aspxparam: { action: index }
member_actions_edit:url: /:module/:id:name.htmlparam: { action: edit }options: segment_separators: [ '/' , '' , '.' ]requirements: id: '\d+'
member_actions:url: /:module/:id/:actionrequirements: id: '\d+'
The Forms
Widgets Validator
用來決定欄位的型態 用來驗證欄位數值
以往採用 helper 的產生方式
<?php echo form_tag('Regist',array('method'=>'POST'))?>
<tr><td><?php echo label_tag($form['name'])?></td>
<td><?php echo input_tag($form['name'])?></td>
</tr>
<tr><td><?php echo label_tag($form['sex'])?></td>
<td><?php echo radio_button_tag($form['sex'])?></td>
</tr>
<?php echo form_tag_end()?>
新的 Form Object
表單欄位型態在 form object 中定義。 表單數值在 form object 中檢查。 將表單的驗證邏輯由 controller 中抽離。 讓表單可以重用。
Form in template(view)
<?php echo form_tag('Regist',array('method'=>'POST'))?><?php echo $Form>renderHiddenFields() ?><?php foreach($Form as $FormField):?><?php if(!$FormField>isHidden()):?> <div> <?php echo $FormField>renderLabel()?> <?php echo $FormField?> <?php if($FormField>hasError()):?> <span><?php echo $FormField>renderError()?></span> <?php endif?> </div><?php endif?><?php endforeach?>
<?php echo form_tag_end()?>
Form in action(controller)
public function executeRegist(sfWebRequest $Request){ $this>Form = new RegisterForm(new Register()); if($Request>isMethod('post')){ $this>Form>bind( $Request>getParameter($this>Form>getName()) ); if($this>Form>isValid()) { $this>Form>save(); } }}
i18n
Symfony 提供了多國語言的支援。 幾個步驟就輕鬆搞定。
Admin Generator
這是一個快速後台產生器。 有用過 Ruby on Rails 或是 Django 的人一
定不陌生。 只需修改 config 就可以作一個具有權限控
管的後台了。
Web Debug Toolbar
Why Symfony?
文件多
http://www.symfonyproject.org/doc/1_4/ A Gentle Introduction to symfony Practical symfony The symfony Reference Book More with symfony
活躍的社群
Mailinglist / forum / IRC More than 1300 plugins , 500 developers. 300K unique visitors per month
on the official web site http://www.symfonyproject.org/
多到不行的 Config
Symfony 預設情況下不修改 Config 就能運作
但是 Config 可以作很多事
cache.yml_list:
enabled: truelifetime: 3600
index:enabled: truelifetime: 3600with_layout: false
security.ymlall:
is_secure: truecredentials: admin
edit:credentials: [admin,edit]
Symfony 被批評的地方
安裝麻煩 ?
安裝方法
安裝 Symfony core libpear channeldiscover pear.symfonyproject.compear install symfony/symfony
建立專案symfony generate:project myproject
沒有解開壓縮檔的步驟,這果然很麻煩。
設定 db schema 好麻煩
從已存在的 DB 中建立 Model
在 DB 宣告好 foreign key 。
匯入 schema.ymlsymfony docteint:buildschema
建立 model , form , filterssymfony doctrine:build –allclasses
不用宣告 has_many , belongs_to 。Table 名稱不用是複數,這好麻煩。
Symfony 效能太差 ?
其實是有解決方法的
請不要用 Symfony
die('Hello World');
Symfony 已經足夠應付許多大型專案
Yahoo! Answers Yahoo! Bookmarks delicious.com
還有疑慮嗎 ?
24 天的實戰教程幫你解除疑惑。
http://www.symfony-project.org/jobeet/1_4/Doctrine/en/
Thanks