Symfony是一个强大的基于PHP的Web开发框架,在这里我们用十分钟的时间来做一个简单的增删改查的程序, 任何不熟悉Symfony的人都可以通过这个教程完成自己的第一个Symfony程序。
如果需要这个样例程序的全部源代码,可以访问 这里 ,或者通过下面的方式获取源代码:
$git clone https://github.com/saharabear/symfony-sample.git
项目初始化
首先,需要你在自己的电脑中安装PHP环境并安装git.这方面的内容属于基础内容,网络上有大量的教程,在这里就不多介绍了,不过要提示的一点是:PHP从5.4开始, 已经内置了测试用服务器,Symfony也拥抱了这个由PHP内置的服务器,只需要在命令行中使用$php app/console server:run 就可以 启动基于Symfony框架的PHP程序进行测试,因此不必要使用XAMPP这一类复杂的集成环境,直接安装PHP并保证在命令行下可以执行php命令就可以了。
然后,我们需要建立一个新的目录,名字叫symfony-sample,Symfony使用一个叫composer的程序管理各种类库的依赖关系,因此如果你的机器上 安装了composer,就可以直接跳过这一步,如果没有安装,可以用下面的命令安装最新版本的composer.
$cd symfony-sample $curl -sS https://getcomposer.org/installer | php
如果希望了解更多关于composer的信息,可以参考这个网站。
安装完成composer后,我们可以开始安装当前最新版本的Symfony2.6.0
复制代码 代码如下:$php composer.phar create-project symfony/framework-standard-edition mysampleproject/ 2.6.0
安装过程中,需要填写数据库等信息,在这个例子中,我们会使用mysql数据库,因此你可以一路按回车键,先不要关心这一切配置应该如何填写。反正 Symfony会在安装成功后,生成一个配置文件,叫app/config/parameters.yml,下面我会提供一个parameters.yml文件的 内容样本,只要复制进去就可以了,先不必关注这么多细节。
刚才创建mysampleproject以后,在symfony-sample目录下生成了mysampleproject目录,我习惯于将程序放在项目的根目录下,因此执行下面的几个命令, 就可以把项目从symfony-sample/mysampleproject目录中,移到symfony-sample目录中
$mv mysampleproject
class Article
{
private $id;
private $title;
private $content;
private $author;
public function getId()
{
return $this->id;
}
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function getTitle()
{
return $this->title;
}
public function setContent($content)
{
$this->content = $content;
return $this;
}
public function getContent()
{
return $this->content;
}
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
public function getAuthor()
{
return $this->author;
}
}
你可以一行不改地使用这些代码。这时候我们再来做几个神奇的操作:
复制代码 代码如下:$php app/console doctrine:schema:update --force
这个操作,已经帮助你通过Article.php建立了数据库和数据表,你不需要自己操作这个过程,下面我们还会对Article.php进行改造,而到时候只需要重新 执行上面的这个操作,Symfony会帮助你自动修改数据库的表结构。
添加约束
上面我们创建了Article.php,既然这个实体代表了具体的业务逻辑,因此我们要考虑几个现实的问题:
1. 用户必须填写标题和内容
2. 用户填写的标题不能超过200个字
3. 用户可以不填写作者
这些就属于业务逻辑,而我们可以修改Article.php如下,以增加相应的业务逻辑的约束:
namespace SymfonyBundleSampleBundleEntity;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
class Article
{
private $id;
private $title;
private $content;
private $author;
public function getId()
{
return $this->id;
}
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function getTitle()
{
return $this->title;
}
public function setContent($content)
{
$this->content = $content;
return $this;
}
public function getContent()
{
return $this->content;
}
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
public function getAuthor()
{
return $this->author;
}
}
然后执行同步数据库的操作:
$ php app/console doctrine:schema:update --force Updating database schema... Database schema updated successfully! "1" queries were executed
增删改查
好了,我们来做一个针对文章的增删改查操作。首先请执行下面的命令:
$ php app/console generate:doctrine:crud Welcome to the Doctrine2 CRUD generator This command helps you generate CRUD controllers and templates. First, you need to give the entity for which you want to generate a CRUD. You can give an entity that does not exist yet and the wizard will help you defining it. You must use the shortcut notation like AcmeBlogBundle:Post. The Entity shortcut name: SymfonySampleBundle:Article By default, the generator creates two actions: list and show. You can also ask it to generate "write" actions: new, update, and delete. Do you want to generate the "write" actions [no]? yes Determine the format to use for the generated CRUD. Configuration format (yml, xml, php, or annotation) [annotation]: yml Determine the routes prefix (all the routes will be "mounted" under this prefix: /prefix/, /prefix/new, ...). Routes prefix [/article]: /article Summary before generation You are going to generate a CRUD controller for "SymfonySampleBundle:Article" using the "yml" format. Do you confirm generation [yes]? yes CRUD generation Generating the CRUD code: OK Generating the Form code: OK You can now start using the generated code!
然后请编辑DefaultController.php中的indexAction如下:
public function indexAction()
{
return array();
}
编辑Resource/views/Default/index.html.twig内容如下:
文章管理
让我们看看神奇的事情,启动内置的测试服务器:
$php app/console server:run
好了,我们已经完成了这十分钟的博客,一切的代码都在Controller/ArticleController.php,Form/ArticleType.php,Resource/views/Article/*.html.twig中,我们已经完成了最基本的文章管理功能。当然在你熟悉Symfony以后,未必需要完全依靠Symfony帮你生成这些增删改查操作,可是起码Symfony用一个命令让一切都先运行起来了,这不就是我们所要的原型吗?
本文永久地址:http://blog.it985.com/5133.html
本文出自 IT985博客 ,转载时请注明出处及相应链接。
更多关于PHP框架相关内容感兴趣的读者可查看本站专题:《php优秀开发框架总结》,《codeigniter入门教程》,《CI(CodeIgniter)框架进阶教程》,《Yii框架入门及常用技巧总结》及《ThinkPHP入门教程》
希望本文所述对大家基于Symfony框架的PHP程序设计有所帮助。



