最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Symfony學(xué)習(xí)十分鐘入門(mén)經(jīng)典教程

 更新時(shí)間:2016年02月03日 14:43:09   作者:Sun  
這篇文章主要介紹了Symfony學(xué)習(xí)十分鐘入門(mén)教程,詳細(xì)介紹了Symfony的安裝配置,項(xiàng)目初始化,建立Bundle,設(shè)計(jì)實(shí)體,添加約束,增刪改查等基本操作技巧,需要的朋友可以參考下

Symfony是一個(gè)強(qiáng)大的基于PHP的Web開(kāi)發(fā)框架,在這里我們用十分鐘的時(shí)間來(lái)做一個(gè)簡(jiǎn)單的增刪改查的程序, 任何不熟悉Symfony的人都可以通過(guò)這個(gè)教程完成自己的第一個(gè)Symfony程序。

如果需要這個(gè)樣例程序的全部源代碼,可以訪問(wèn) 這里 ,或者通過(guò)下面的方式獲取源代碼:

$git clone https://github.com/saharabear/symfony-sample.git

項(xiàng)目初始化

首先,需要你在自己的電腦中安裝PHP環(huán)境并安裝git.這方面的內(nèi)容屬于基礎(chǔ)內(nèi)容,網(wǎng)絡(luò)上有大量的教程,在這里就不多介紹了,不過(guò)要提示的一點(diǎn)是:PHP從5.4開(kāi)始, 已經(jīng)內(nèi)置了測(cè)試用服務(wù)器,Symfony也擁抱了這個(gè)由PHP內(nèi)置的服務(wù)器,只需要在命令行中使用$php app/console server:run 就可以 啟動(dòng)基于Symfony框架的PHP程序進(jìn)行測(cè)試,因此不必要使用XAMPP這一類復(fù)雜的集成環(huán)境,直接安裝PHP并保證在命令行下可以執(zhí)行php命令就可以了。

然后,我們需要建立一個(gè)新的目錄,名字叫symfony-sample,Symfony使用一個(gè)叫composer的程序管理各種類庫(kù)的依賴關(guān)系,因此如果你的機(jī)器上 安裝了composer,就可以直接跳過(guò)這一步,如果沒(méi)有安裝,可以用下面的命令安裝最新版本的composer.

$cd symfony-sample
$curl -sS https://getcomposer.org/installer | php

如果希望了解更多關(guān)于composer的信息,可以參考這個(gè)網(wǎng)站

安裝完成composer后,我們可以開(kāi)始安裝當(dāng)前最新版本的Symfony2.6.0

復(fù)制代碼 代碼如下:
$php composer.phar create-project symfony/framework-standard-edition mysampleproject/ 2.6.0

安裝過(guò)程中,需要填寫(xiě)數(shù)據(jù)庫(kù)等信息,在這個(gè)例子中,我們會(huì)使用mysql數(shù)據(jù)庫(kù),因此你可以一路按回車(chē)鍵,先不要關(guān)心這一切配置應(yīng)該如何填寫(xiě)。反正 Symfony會(huì)在安裝成功后,生成一個(gè)配置文件,叫app/config/parameters.yml,下面我會(huì)提供一個(gè)parameters.yml文件的 內(nèi)容樣本,只要復(fù)制進(jìn)去就可以了,先不必關(guān)注這么多細(xì)節(jié)。

剛才創(chuàng)建mysampleproject以后,在symfony-sample目錄下生成了mysampleproject目錄,我習(xí)慣于將程序放在項(xiàng)目的根目錄下,因此執(zhí)行下面的幾個(gè)命令, 就可以把項(xiàng)目從symfony-sample/mysampleproject目錄中,移到symfony-sample目錄中

$mv mysampleproject/* ./
$rm -rf mysampleproject

理論上來(lái)講,我們已經(jīng)完成了Symfony項(xiàng)目的創(chuàng)建,不過(guò)剛才提到的parameters.yml文件還沒(méi)有解釋。這個(gè)parameters.yml是Symfony的全局配置文件, 無(wú)論是數(shù)據(jù)庫(kù)配置信息還是其他的各種配置,都可以放在這個(gè)文件中。下面是我們需要使用的測(cè)試用的parameters.yml,記得把最后一行的值修改為一個(gè)隨機(jī)值

# This file is auto-generated during the composer install
parameters:
  database_driver: pdo_mysql
  database_host: localhost
  database_port: 3306
  database_name: symfony
  database_user: root
  database_password: root
  mailer_transport: smtp
  mailer_host: localhost
  mailer_user: null
  mailer_password: null
  locale: en
  secret: ChangeThisLineAsYouWish_ioiuqwoieru

直接用這段,替換掉app/config/parameters.yml文件中的內(nèi)容,然后編輯app/config/config.yml,找到下面幾行,把最后一行添加進(jìn)去并保存。

driver:  "%database_driver%"
host:   "%database_host%"
port:   "%database_port%"
dbname:  "%database_name%"
user:   "%database_user%"
password: "%database_password%"
charset: UTF8
path:   "%database_path%"

好了,這樣我們就完成了基本的Symfony程序的配置,你現(xiàn)在有了一個(gè)配置好了數(shù)據(jù)庫(kù),郵件發(fā)送器,日志系統(tǒng)的基本程序原型。下面,我們就開(kāi)始編寫(xiě)自己的Symfony程序。

建立Bundle

先說(shuō)一下什么是Bundle。Symfony是以DI為核心的,可能你不知道什么是DI,沒(méi)關(guān)系,這不重要,你可以把Symfony的DI理解成為一個(gè)功能池,把程序中的所有功能都做成Bundle,或者你把Bundle理解成一組php文件組合而成的程序就可以。 比如用戶注冊(cè),登錄功能做成一個(gè)Bundle,你也可以把一個(gè)論壇的發(fā)帖回貼功能做成一個(gè)Bundle,自然也可以把文章管理做成一個(gè)Bundle,然后用一個(gè)Bundle去調(diào)用和配置不同的Bundle,那么你就可以把網(wǎng)站組裝起來(lái)了,而你寫(xiě)的各種Bundle,在其他的應(yīng)用程序中還可以繼續(xù)復(fù)用,這樣寫(xiě)的Bundle越多,可復(fù)用性就越強(qiáng),制作新項(xiàng)目的時(shí)候也越有利。

我們現(xiàn)在就來(lái)建立自己的Bundle.在命令行中,使用命令:

$php app/console generate:bundle
Bundle namespace: Symfony/Bundle/SampleBundle
Bundle name [SymfonySampleBundle]:
Target directory [/home/saharabear/workspace/symfony-sample/src]:
Configuration format (yml, xml, php, or annotation): yml
Do you want to generate the whole directory structure [no]? yes
Do you confirm generation [yes]? yes
Generating the bundle code: OK
Checking that the bundle is autoloaded: OK
Confirm automatic update of your Kernel [yes]? yes
Enabling the bundle inside the Kernel: OK
Confirm automatic update of the Routing [yes]? yes

這樣就成功建立了我們的Bundle,名字叫SymfonySampleBundle,我們使用的Bundle namespace是Symfony/Bundle/SampleBundle,這是一種約定,我們還可以建立其他的Bundle,比如Symfony/Bundle/PostBundle, 或者Symfony/Bundle/ArticleBundle,而對(duì)應(yīng)的Bundle name就分別是SymfonyPostBundle或者SymfonyArticleBundle。你也可以自己建立這幾個(gè)Bundle,這并不會(huì)影響當(dāng)前我們的教程。

對(duì)了,在我們建立的Bundle中,分別會(huì)生成下面幾個(gè)目錄:

① Entity:這個(gè)目錄并不是必須的,很多情況下只有在生成實(shí)體的時(shí)候才會(huì)生成,放置模型,也就是MVC中的M
② Controller:這個(gè)目錄會(huì)生成DefaultController.php,你可以在這里建立自己的Controller控制器,也就是MVC中的C
③ Resources:這個(gè)目錄下面還有子目錄,其中views放置的是模板,也就是MVC中的V,而public放置的是靜態(tài)文件,比如js, css, images等等
④ Tests:放置單元測(cè)試與集成測(cè)試的代碼,在這個(gè)樣例程序中暫時(shí)不需要
⑤ DependencyInjection:與DI相關(guān)的目錄,暫時(shí)也不需要去了解
⑥ SymfonySampleBundle.php:當(dāng)前這個(gè)Bundle的定義文件

更多細(xì)節(jié)可以去閱讀Symfony 的官方文檔,而當(dāng)前的重點(diǎn)是把這個(gè)Symfony的樣例程序運(yùn)行起來(lái)。

設(shè)計(jì)實(shí)體

在MVC的設(shè)計(jì)理念中,M是最重要的,因?yàn)镸表達(dá)的內(nèi)容是業(yè)務(wù)邏輯。我覺(jué)得如果這個(gè)地方往深入去探討,會(huì)一直探討到富血模型或者貧血模型,不過(guò)目前在這個(gè)教程中根本 不需要考慮這么多,你只需要知道實(shí)體就是MVC中的M,用于表達(dá)業(yè)務(wù)邏輯。比如說(shuō),我們要開(kāi)發(fā)一個(gè)文章管理的系統(tǒng),那么文章本身就代表的業(yè)務(wù)邏輯。比如,我們的文章要有 標(biāo)題,內(nèi)容,作者,那么這三項(xiàng)就屬于業(yè)務(wù)邏輯,而標(biāo)題不能夠?yàn)榭眨荒艹^(guò)200長(zhǎng)度,內(nèi)容不能為空,作者卻是可以為空的,這些也屬于業(yè)務(wù)邏輯。同時(shí),這個(gè)文章需要被 存儲(chǔ)起來(lái),比如存儲(chǔ)到數(shù)據(jù)庫(kù)中,那么這個(gè)M就應(yīng)該能夠映射到數(shù)據(jù)庫(kù)的表中。我們把這個(gè)M,叫實(shí)體。

還是少說(shuō)廢話,直接上代碼。那么如何建立實(shí)體呢?當(dāng)然不是從頭一點(diǎn)一點(diǎn)地寫(xiě),而是直接用下面的命令生成:

$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!

經(jīng)過(guò)這些命令,你會(huì)發(fā)現(xiàn)在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;
  }
}

你可以一行不改地使用這些代碼。這時(shí)候我們?cè)賮?lái)做幾個(gè)神奇的操作:

復(fù)制代碼 代碼如下:
$php app/console doctrine:schema:update --force

這個(gè)操作,已經(jīng)幫助你通過(guò)Article.php建立了數(shù)據(jù)庫(kù)和數(shù)據(jù)表,你不需要自己操作這個(gè)過(guò)程,下面我們還會(huì)對(duì)Article.php進(jìn)行改造,而到時(shí)候只需要重新 執(zhí)行上面的這個(gè)操作,Symfony會(huì)幫助你自動(dòng)修改數(shù)據(jù)庫(kù)的表結(jié)構(gòu)。

添加約束

上面我們創(chuàng)建了Article.php,既然這個(gè)實(shí)體代表了具體的業(yè)務(wù)邏輯,因此我們要考慮幾個(gè)現(xiàn)實(shí)的問(wèn)題:

1. 用戶必須填寫(xiě)標(biāo)題和內(nèi)容
2. 用戶填寫(xiě)的標(biāo)題不能超過(guò)200個(gè)字
3. 用戶可以不填寫(xiě)作者

這些就屬于業(yè)務(wù)邏輯,而我們可以修改Article.php如下,以增加相應(yīng)的業(yè)務(wù)邏輯的約束:

namespace Symfony\Bundle\SampleBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
 * 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
   * @Assert\NotBlank(message="標(biāo)題不可為空")
   * @Assert\Length(
   *   max=200,
   *   maxMessage="標(biāo)題不能超過(guò)200個(gè)字"
   * )
   * @ORM\Column(name="title", type="string", length=200)
   */
  private $title;
  /**
   * @var string
   *
   * @Assert\NotBlank(message="文章內(nèi)容不可為空")
   * @ORM\Column(name="content", type="text")
   */
  private $content;
  /**
   * @var string
   *
   * @ORM\Column(name="author", type="string", length=20,nullable=true)
   */
  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;
  }
}

然后執(zhí)行同步數(shù)據(jù)庫(kù)的操作:

$ php app/console doctrine:schema:update --force
Updating database schema...
Database schema updated successfully! "1" queries were executed

增刪改查

好了,我們來(lái)做一個(gè)針對(duì)文章的增刪改查操作。首先請(qǐng)執(zhí)行下面的命令:

$ 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!

然后請(qǐng)編輯DefaultController.php中的indexAction如下:

/**
 * @Route("/",name="welcome")
 * @Template()
 */
public function indexAction()
{
  return array();
}

編輯Resource/views/Default/index.html.twig內(nèi)容如下:

<a href="{{path('article')}}">文章管理</a>

讓我們看看神奇的事情,啟動(dòng)內(nèi)置的測(cè)試服務(wù)器:

$php app/console server:run

好了,我們已經(jīng)完成了這十分鐘的博客,一切的代碼都在Controller/ArticleController.php,Form/ArticleType.php,Resource/views/Article/*.html.twig中,我們已經(jīng)完成了最基本的文章管理功能。當(dāng)然在你熟悉Symfony以后,未必需要完全依靠Symfony幫你生成這些增刪改查操作,可是起碼Symfony用一個(gè)命令讓一切都先運(yùn)行起來(lái)了,這不就是我們所要的原型嗎?

本文永久地址:http://blog.it985.com/5133.html
本文出自 IT985博客 ,轉(zhuǎn)載時(shí)請(qǐng)注明出處及相應(yīng)鏈接。

更多關(guān)于PHP框架相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》,《codeigniter入門(mén)教程》,《CI(CodeIgniter)框架進(jìn)階教程》,《Yii框架入門(mén)及常用技巧總結(jié)》及《ThinkPHP入門(mén)教程

希望本文所述對(duì)大家基于Symfony框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Laravel 5 框架入門(mén)(二)構(gòu)建 Pages 的管理功能

    Laravel 5 框架入門(mén)(二)構(gòu)建 Pages 的管理功能

    這篇文章主要介紹了Laravel 5 框架入門(mén)的第二篇文章,給大家講解的是構(gòu)建 Pages 的管理功能,十分的詳細(xì),有需要的小伙伴可以參考下。
    2015-04-04
  • Laravel創(chuàng)建數(shù)據(jù)庫(kù)表結(jié)構(gòu)的例子

    Laravel創(chuàng)建數(shù)據(jù)庫(kù)表結(jié)構(gòu)的例子

    今天小編就為大家分享一篇Laravel創(chuàng)建數(shù)據(jù)庫(kù)表結(jié)構(gòu)的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-10-10
  • ThinkPHP3.2框架自帶分頁(yè)功能實(shí)現(xiàn)方法示例

    ThinkPHP3.2框架自帶分頁(yè)功能實(shí)現(xiàn)方法示例

    這篇文章主要介紹了ThinkPHP3.2框架自帶分頁(yè)功能實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了thinkPHP框架自帶分頁(yè)功能相關(guān)布局、樣式及后臺(tái)操作實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-05-05
  • thinkPHP內(nèi)置字符串截取函數(shù)用法詳解

    thinkPHP內(nèi)置字符串截取函數(shù)用法詳解

    這篇文章主要介紹了thinkPHP內(nèi)置字符串截取函數(shù)用法,結(jié)合實(shí)例形式分析了thinkPHP內(nèi)置的字符串截取函數(shù)功能與具體使用方法,以及針對(duì)無(wú)法添加省略號(hào)的源碼修改技巧,需要的朋友可以參考下
    2016-11-11
  • php插入mysql數(shù)據(jù)返回id的方法

    php插入mysql數(shù)據(jù)返回id的方法

    今天小編就為大家分享一篇php插入mysql數(shù)據(jù)返回id的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • ThinkPHP連接數(shù)據(jù)庫(kù)的方式匯總

    ThinkPHP連接數(shù)據(jù)庫(kù)的方式匯總

    這篇文章主要介紹了ThinkPHP連接數(shù)據(jù)庫(kù)的方式,包括項(xiàng)目配置文件定義、DSN方式傳參、數(shù)組傳參、模型類里定義等,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-12-12
  • php 的反射詳解及示例代碼

    php 的反射詳解及示例代碼

    本文主要介紹PHP的反射內(nèi)容的知識(shí),這里提供相關(guān)的資料講解,及簡(jiǎn)單示例代碼供大家參考,有興趣的小伙伴可以參考下
    2016-08-08
  • php遞歸使用示例(php遞歸函數(shù))

    php遞歸使用示例(php遞歸函數(shù))

    這篇文章主要介紹了php遞歸使用示例(php遞歸函數(shù)),包括遞歸獲得角色I(xiàn)D字符串、遞歸獲取級(jí)聯(lián)角色信息數(shù)組、通過(guò)父角色的id獲取子角色信息,需要的朋友可以參考下
    2014-02-02
  • php中對(duì)內(nèi)置函數(shù)json_encode和json_decode的異常處理

    php中對(duì)內(nèi)置函數(shù)json_encode和json_decode的異常處理

    這篇文章主要介紹了php中對(duì)內(nèi)置函數(shù)json_encode和json_decode的異常處理,本篇文章通過(guò)定義描述和列舉表格以及最后代碼的展示,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • 微信小程序發(fā)送訂閱消息的方法(php 為例)

    微信小程序發(fā)送訂閱消息的方法(php 為例)

    小程序訂閱消息公測(cè)已經(jīng)有些日子,今天以世界上最好的語(yǔ)言(PHP)為例,說(shuō)一下如何發(fā)送訂閱消息。感興趣的朋友跟隨小編一起看看吧
    2019-10-10

最新評(píng)論

德化县| 开封市| 扶余县| 土默特左旗| 岢岚县| 安庆市| 宜黄县| 固始县| 南丰县| 会昌县| 柳林县| 定西市| 庆阳市| 张掖市| 徐闻县| 古浪县| 武穴市| 辽宁省| 会理县| 霍林郭勒市| 高平市| 萍乡市| 廊坊市| 札达县| 云霄县| 治多县| 民权县| 定结县| 铜陵市| 罗源县| 当雄县| 乌审旗| 项城市| 丹江口市| 新乐市| 自治县| 金溪县| 东安县| 格尔木市| 阿图什市| 长乐市|