這裏搭建一個簡單的博客系統做爲練習,以後再完成學校任務搭建一個表白牆,php
使用htmlpurifier和parsedown來搭建前端,因此須要先安裝這兩個第三方包,必需要弄一個composer的國內鏡像,否則安裝的實在是太慢了,html
composer config repo.packagist composer https://packagist.phpcomposer.com 局部更新的方法,全局更新可能會出問題前端
更新以後使用 composer require "包的名稱,如"composer require erusev/parsedown "^1.6" -vvv「,以後即可以進行使用mysql
Markdown是一種輕量級語言,用來將文本輸出成html代碼的,先對HTMLpurilier進行學習吧web
.導入文件這裏沒有必要,由於已經下載下來了,sql
.獲取默認配置 $config = HTMLPurilier_Config::createDeafult();數據庫
.生成過濾器實體 $html_purilier = new HTMLPurilier($config);markdown
過濾html代碼,防止xss攻擊 $clean_html = $html_purilier->purify($dirty_html); app
下面說明一下具體配置composer
$config->set('配置屬性的名稱',value,a=null) 具體有哪些屬性等用的時候去官網查看就行
第一步先編寫Markdown類,用於過濾並生成html代碼的類,代碼這裏不放了,書寫完成以後須要在services之中進行註冊,否則沒法使用
# the slugger service needs a public alias for getting it from # the container when loading doctrine fixtures slugger: symfony3.4註冊方法 alias: AppBundle\Utils\Slugger public: true
接下來進行twig模板擴展的編寫,注意一下這是官網上給的方法,abstractExtension繼承了以前的\Twig_Extension類,因此這裏是同樣的至關於
<?php /** * Created by PhpStorm. * User: 亦清 * Date: 2019/3/13 * Time: 20:06 */ namespace AppBundle\Twig; use AppBundle\Utils\Markdown; use Twig\Extension\AbstractExtension; use Twig\TwigFilter; //放置頭一個擴展的類 class AppExtension extends AbstractExtension { /** * @var Markdown */ private $parser; public function __construct(Markdown $parser) { $this->parser = $parser; //使用markdown來初始化,用於後面的過濾 } /** * 將父類的註釋都繼承下來 * {@inheritdoc} */ public function getFilters(){ //twig模板過濾 return [ new TwigFilter('md2html',[$this,'markdownToHtml']), ]; } /** * 過濾函數 * @param string $content * @return string */ public function markdownToHtml($content){ $this->parser->toHtml($content); return $content; } /** *{@inheritdoc} */ public function getName(){ return "app.extension"; } }
編寫完成以後進行註冊
app.twig_extension: #註冊擴展appExtension擴展 public: false class: AppBundle\Twig\AppExtension arguments: ['@markdown'] tags: -{name: twig.extension}
接下來書寫Entity實體,post和category這兩個,這裏簡單起見,他們字段都很少,其中post的category和Category是多對一的關係,須要設置外鍵進行鏈接,其餘的沒有什麼
以後使用doctring:generate:database 建立數據庫 再使用doctrine:scheam:validate檢查annoation是否正確,以後使用doctrine:generate:entities AppBundle產生實體,再而後使用
doctrine:schema:update 將數據實例化到數據庫當中去就好了,比較簡單這裏就不放代碼了,
接下來使用DoctrineFixturesBundle來進行初始化數據庫的操做,使用以前須要使用
composer require --dev doctrine/doctrine-fixtures-bundle
來載入這個bundle,至於這個模塊的使用,這裏不進行說明了,官方文檔有,我有一篇文章進行了說明
在此以後,須要對其進行初始化,
初始化操做分爲兩個category文件和post文件,這裏除了以前說的,還有兩個組件須要使用
use Symfony\Component\DependencyInjection\ContainerAwareInterface; //容器組件,將東西注入到容器當中,以後即可以使用
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Finder\Finder; //symfony目錄組件,遍歷組件,取出文件內容
這裏使用Container是爲了將三個文件當中的內容獲取出來,以後使用symfony的Finder文件目錄來將其中的內容獲取出來,用來初始化數據庫具體用法以下
/** * @var ContainerInterface */
private $container; /** * 構造方法構建一個容器, * @Param ContainerInterface $container */
public function setContainer(ContainerInterface $container = null) { $this->container =$container; }
public function load(ObjectManager $manager)
{
$posts = array(
['title' => '第一講:Symfony3的簡介,開發環境與版本控制', 'cate' => 'symfony3-practis'],
['title' => '第二講: 最佳實踐與第一個Symfony應用', 'cate' => 'symfony3-practis'],
['title' => '第三講: 建立初步用戶系統', 'cate' => 'symfony3-practis'],
);
$post_file_path = $this->container->getParameter('kernel.root_dir').'/data/fixtures/posts'; //用容器的get方法獲取文件目錄
$find = new Finder(); //使用symfony目錄對象遍歷組件進行文件遍歷,
$find->files()->in($post_file_path);
$i = 0;
foreach($find as $file){ //遍歷文件獲取其中內容,放入到posts當中
$posts[$i]['content'] =$file->getContents();
$i++;
}
foreach($posts as $post){
$product = new Post();
$product->setTitle($post['title']);
$product->setContent($post['content']);
$product->setCategory($this->getReference('category-'.$post['cate'])); //設置其分類,使用getReference
$manager->persist($product);
$manager->flush();
}
}
以後使用命令
php bin/cosole doctrine:fixtures:load 進行數據庫初始化操做,以後在mysql查看是否正確初始化數據
以後 須要書寫twig模板,定義各個頁面之間的關係,這裏先寫一下twig的具體使用方法吧, 感受以前學這塊時候太粗糙了