標籤:
http://blog.csdn.net/kunshan_shenbin/article/details/7164675
參考自:http://xsymfony.801.cxne.net/forum.php?mod=viewthread&tid=12&rpid=459&page=1
在建立項目之前,首先需要搭建symfony發開環境。http://blog.csdn.net/kunshan_shenbin/article/details/7162243
1. 建立工作目錄,產生專案檔
>md cms
>cd cms
>symfony generate:project cms
>symfony generate:app frontend
>symfony generate:app backend
2. Apache下設定項目(建立虛擬機器主機)
[html] view plaincopy
- <VirtualHost *:1300>
- DocumentRoot "D:\Work\PHP\cms\web"
- DirectoryIndex index.php
- <Directory "D:\Work\PHP\cms\web">
- AllowOverride All
- Allow from All
- </Directory>
-
- Alias /sf D:\xampp\php\data\symfony\web\sf
- <Directory "D:\xampp\php\data\symfony\web\sf">
- AllowOverride All
- Allow from All
- </Directory>
- </VirtualHost>
注意:
以上配置需要添加在httpd-vhosts.conf檔案中,並在httpd.conf中開啟對相應連接埠的監聽。
當然,我們也可以通過修改hosts檔案添加相應的網域名稱解析。這裡略過( 使用localhost ) 。
重啟Apache後,通過訪問http://localhost:1300/ 可以看到symfony工程歡迎頁面。
從這裡開始,我們可以選擇一個順手的IDE開啟工程,以便更好的發開項目。
3. 配置並建立資料庫
開啟工程下config中的databases.yml檔案,修改資料庫連接的參數。
[html] view plaincopy
- # You can find more information about this file on the symfony website:
- # http://www.symfony-project.org/reference/1_4/en/07-Databases
-
- all:
- doctrine:
- class: sfDoctrineDatabase
- param:
- dsn: mysql:host=localhost;dbname=cms
- username: root
- password: root
定義schema ( cms/config/doctrine/schema.yml )
[html] view plaincopy
- Category:
- columns:
- name: string(50)
- description: string(1000)
-
- Content:
- actAs:
- Timestampable: ~
- columns:
- title: string(255)
- body: clob
- view_count: integer
- recommend_level:
- type: enum
- values: [0, 1, 2]
- default: 2
- category_id: integer
- relations:
- Category:
- local: category_id
- foreign: id
- foreignAlias: Contents
-
- Comment:
- columns:
- body: clob
- user_id: integer
- content_id: integer
- relations:
- Content:
- local: content_id
- foreign: id
- foreignAlias: Comments
運行如下指令:
>symfony doctrine:build --all
4. 匯入測試資料
開啟cms/data/fixtures/fixtures.yml檔案,輸入測試資料
[html] view plaincopy
- # # Populate this file with data to be loaded by your ORM‘s *:data-load task.
- # # You can create multiple files in this directory (i.e. 010_users.yml,
- # # 020_articles.yml, etc) which will be loaded in alphabetical order.
- # #
- # # See documentation for your ORM‘s *:data-load task for more information.
- #
- # User:
- # fabien:
- # username: fabien
- # password: changeme
- # name: Fabien Potencier
- # email: [email protected]
- # kris:
- # username: Kris.Wallsmith
- # password: changeme
- # name: Kris Wallsmith
- # email: [email protected]
-
- Category:
- c1:
- name: 巴西
- description: 南美球隊
- c2:
- name: 英國
- description: 歐洲球隊
- c3:
- name: 加納
- description: 非洲球隊
-
- Content:
- t1:
- title: 卡卡助攻
- body: ......
- view_count: 6
- recommend_level: 0
- Category: c1
- Comments: [m1, m2]
- t2:
- title: 魯尼沒有大作為
- body: ......
- view_count: 10
- recommend_level: 1
- Category: c2
-
- Comment:
- m1:
- body: 很贊
- m2:
- body: 太不盡人意了。
運行如下指令:
> symfony doctrine:data-load
5. 接下來我們開始著手產生背景管理頁面:
>symfony doctrine:generate-admin backend Category
>symfony doctrine:generate-admin backend Content
>symfony doctrine:generate-admin backend Comment
通過如下地址訪問頁面(開發環境入口)
http://localhost:1300/backend_dev.php/category
http://localhost:1300/backend_dev.php/content
http://localhost:1300/backend_dev.php/comment
然後運行如下指令添加css等樣式資源:
>symfony plugin:publish-assets
再次訪問後頁面會比原來漂亮很多。
在windows上使用symfony建立簡易的CMS系統(一)