在windows上使用symfony建立簡易的CMS系統(一)

來源:互聯網
上載者:User

標籤:

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 
  1. <VirtualHost *:1300>  
  2.     DocumentRoot "D:\Work\PHP\cms\web"  
  3.     DirectoryIndex index.php  
  4.     <Directory "D:\Work\PHP\cms\web">  
  5.         AllowOverride All  
  6.         Allow from All  
  7.     </Directory>  
  8.   
  9.     Alias /sf D:\xampp\php\data\symfony\web\sf  
  10.     <Directory "D:\xampp\php\data\symfony\web\sf">  
  11.         AllowOverride All  
  12.         Allow from All  
  13.     </Directory>  
  14. </VirtualHost>  

注意:

 

以上配置需要添加在httpd-vhosts.conf檔案中,並在httpd.conf中開啟對相應連接埠的監聽。

當然,我們也可以通過修改hosts檔案添加相應的網域名稱解析。這裡略過( 使用localhost ) 。

重啟Apache後,通過訪問http://localhost:1300/ 可以看到symfony工程歡迎頁面。


從這裡開始,我們可以選擇一個順手的IDE開啟工程,以便更好的發開項目。

3. 配置並建立資料庫

開啟工程下config中的databases.yml檔案,修改資料庫連接的參數。

 

[html] view plaincopy 
  1. # You can find more information about this file on the symfony website:  
  2. # http://www.symfony-project.org/reference/1_4/en/07-Databases  
  3.   
  4. all:  
  5.   doctrine:  
  6.     class: sfDoctrineDatabase  
  7.     param:  
  8.       dsn:      mysql:host=localhost;dbname=cms  
  9.       username: root  
  10.       password: root  


定義schema ( cms/config/doctrine/schema.yml )

 

 

[html] view plaincopy 
  1. Category:  
  2.   columns:  
  3.     name: string(50)  
  4.     description: string(1000)  
  5.       
  6. Content:  
  7.   actAs:  
  8.     Timestampable: ~  
  9.   columns:  
  10.     title: string(255)  
  11.     body: clob  
  12.     view_count: integer  
  13.     recommend_level:  
  14.       type: enum  
  15.       values: [0, 1, 2]  
  16.       default: 2  
  17.     category_id: integer  
  18.   relations:  
  19.     Category:  
  20.       local: category_id  
  21.       foreign: id  
  22.       foreignAlias: Contents  
  23.         
  24. Comment:  
  25.   columns:  
  26.     body: clob  
  27.     user_id: integer  
  28.     content_id: integer  
  29.   relations:  
  30.     Content:  
  31.       local: content_id  
  32.       foreign: id  
  33.       foreignAlias: Comments  


運行如下指令:

 

>symfony doctrine:build --all

 

4. 匯入測試資料

開啟cms/data/fixtures/fixtures.yml檔案,輸入測試資料

[html] view plaincopy 
  1. # # Populate this file with data to be loaded by your ORM‘s *:data-load task.  
  2. # # You can create multiple files in this directory (i.e. 010_users.yml,  
  3. # # 020_articles.yml, etc) which will be loaded in alphabetical order.  
  4. # #   
  5. # # See documentation for your ORM‘s *:data-load task for more information.  
  6. #   
  7. # User:  
  8. #   fabien:  
  9. #     username: fabien  
  10. #     password: changeme  
  11. #     name:     Fabien Potencier  
  12. #     email:    [email protected]  
  13. #   kris:  
  14. #     username: Kris.Wallsmith  
  15. #     password: changeme  
  16. #     name:     Kris Wallsmith  
  17. #     email:    [email protected]  
  18.   
  19. Category:  
  20.   c1:  
  21.     name: 巴西  
  22.     description: 南美球隊  
  23.   c2:  
  24.     name: 英國  
  25.     description: 歐洲球隊  
  26.   c3:  
  27.     name: 加納  
  28.     description: 非洲球隊  
  29.       
  30. Content:  
  31.   t1:  
  32.     title: 卡卡助攻  
  33.     body: ......  
  34.     view_count: 6  
  35.     recommend_level: 0  
  36.     Category: c1  
  37.     Comments: [m1, m2]  
  38.   t2:  
  39.     title: 魯尼沒有大作為  
  40.     body: ......  
  41.     view_count: 10  
  42.     recommend_level: 1  
  43.     Category: c2  
  44.       
  45. Comment:  
  46.   m1:  
  47.    body: 很贊  
  48.   m2:  
  49.    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系統(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.