在windows上使用symfony创建简易的CMS系统(一)
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下配置项目(新建虚拟主机)
- <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文件,修改数据库连接的参数。
- # 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 )
- 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文件,输入测试数据
- # # 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: fabien.potencier@symfony-project.com
- # kris:
- # username: Kris.Wallsmith
- # password: changeme
- # name: Kris Wallsmith
- # email: kris.wallsmith@symfony-project.com
- 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
再次访问后页面会比原来漂亮很多。