symfony简单的博客练习,熟悉具体开发流程

这里搭建一个简单的博客系统做为练习,以后再完成学校任务搭建一个表白墙,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的具体使用方法吧, 感受以前学这块时候太粗糙了

相关文章
相关标签/搜索