Symfony学习十分钟入门经典教程

内容摘要
Symfony是一个强大的基于PHP的Web开发框架,在这里我们用十分钟的时间来做一个简单的增删改查的程序, 任何不熟悉Symfony的人都可以通过这个教程完成自己的第一个Symfony程序
文章正文

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

Symfony 的官方文档,而当前的重点是把这个Symfony的样例程序运行起来。

设计实体

在MVC的设计理念中,M是最重要的,因为M表达的内容是业务逻辑。我觉得如果这个地方往深入去探讨,会一直探讨到富血模型或者贫血模型,不过目前在这个教程中根本 不需要考虑这么多,你只需要知道实体就是MVC中的M,用于表达业务逻辑。比如说,我们要开发一个文章管理的系统,那么文章本身就代表的业务逻辑。比如,我们的文章要有 标题,内容,作者,那么这三项就属于业务逻辑,而标题不能够为空,不能超过200长度,内容不能为空,作者却是可以为空的,这些也属于业务逻辑。同时,这个文章需要被 存储起来,比如存储到数据库中,那么这个M就应该能够映射到数据库的表中。我们把这个M,叫实体。

还是少说废话,直接上代码。那么如何建立实体呢?当然不是从头一点一点地写,而是直接用下面的命令生成:

$php app/console generate:doctrine:entity
Welcome to the Doctrine2 entity generator
This command helps you generate Doctrine2 entities.
First, you need to give the entity name you want to generate.
You must use the shortcut notation like AcmeBlogBundle:Post.
The Entity shortcut name: SymfonySampleBundle:Article
Determine the format to use for the mapping information.
Configuration format (yml, xml, php, or annotation) [annotation]:yml
Instead of starting with a blank entity, you can add some fields now.
Note that the primary key will be added automatically (named id).
Available types: array, simple_array, json_array, object,
boolean, integer, smallint, bigint, string, text, datetime, datetimetz,
date, time, decimal, float, blob, guid.
New field name (press to stop adding fields): title
Field type [string]:
Field length [255]: 200
New field name (press to stop adding fields): content
Field type [string]: text
New field name (press to stop adding fields): author
Field type [string]:
Field length [255]: 20
New field name (press to stop adding fields):
Do you want to generate an empty repository class [no]? yes
Summary before generation
You are going to generate a "SymfonySampleBundle:Article" Doctrine2 entity
using the "yml" format.
Do you confirm generation [yes]? yes
Entity generation
Generating the entity code: OK
You can now start using the generated code!

经过这些命令,你会发现在Entity中建立了新的文件Article.php,代码如下:

namespace Symfony\Bundle\SampleBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
 * Article
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Symfony\Bundle\SampleBundle\Entity\ArticleRepository")
 */
class Article
{
  /**
   * @var integer
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;
  /**
   * @var string
   *
   * @ORM\Column(name="title", type="string", length=200)
   */
  private $title;
  /**
   * @var string
   *
   * @ORM\Column(name="content", type="text")
   */
  private $content;
  /**
   * @var string
   *
   * @ORM\Column(name="author", type="string", length=20)
   */
  private $author;
  /**
   * Get id
   *
   * @return integer
   */
  public function getId()
  {
    return $this->id;
  }
  /**
   * Set title
   *
   * @param string $title
   * @return Article
   */
  public function setTitle($title)
  {
    $this->title = $title;
    return $this;
  }
  /**
   * Get title
   *
   * @return string
   */
  public function getTitle()
  {
    return $this->title;
  }
  /**
   * Set content
   *
   * @param string $content
   * @return Article
   */
  public function setContent($content)
  {
    $this->content = $content;
    return $this;
  }
  /**
   * Get content
   *
   * @return string
   */
  public function getContent()
  {
    return $this->content;
  }
  /**
   * Set author
   *
   * @param string $author
   * @return Article
   */
  public function setAuthor($author)
  {
    $this->author = $author;
    return $this;
  }
  /**
   * Get author
   *
   * @return string
   */
  public function getAuthor()
  {
    return $this->author;
  }
}

你可以一行不改地使用这些代码。这时候我们再来做几个神奇的操作:

php优秀开发框架总结》,《codeigniter入门教程》,《CI(CodeIgniter)框架进阶教程》,《Yii框架入门及常用技巧总结》及《ThinkPHP入门教程》

希望本文所述对大家基于Symfony框架的PHP程序设计有所帮助。


代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!

© 2020 IDC笔记 . | 备案号:辽ICP备18000516号