symfony3 常用命令以及 监听事件,订阅事件

文章详细介绍了在Symfony3中创建数据表、更新数据库、生成表单及事件监听的步骤。通过注册监听服务、定义事件类和监听器,实现了对特定事件的响应。此外,还讨论了如何重写AppBundle以及在Windows下创建软链接的方法。
摘要由CSDN通过智能技术生成

symfony3 一些命令整理:

创建一个数据表实例: 两步

1.php bin/console doctrine:generate:entity
2. AppBundle:Goods

把创建好的 数据表 执行到 数据库

php bin/console doctrine:schema:update --force

生成form 表单

php bin/console generate:doctrine:form AcmeBlogBundle:Post

事件监听 订阅

先在 services.yaml 文件下 注册监听服务

1.  my.listener:
2.        class: AppBundle\Listener\ContentSubmitListener
3.        tags: 
            - {name: 'kernel.event_listener',event: AppBundle\Event\AfterContentSubmitEvent, method: 'onContentSubmit'}

解释:
1:是要注册名字是什么,或者说 要注册的服务
2:就是类名,监听事件的文件名字
通过 tags 标签,告诉 框架,这是一个 针对 什么的 事件监听
name:要监听的事件,event:要监听的事件的对象是哪一个,event文件下的。
methon:2->class类里面的方法下的 方法名,如果没有写 method的话,默认是 on+驼峰事件名

事件监听:

文件:src/AppBundle/Event/AfterContentSubmitEvent.php

namespace AppBundle\Event;
use AppBundle\Entity\Goods;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\GenericEvent;
class AfterContentSubmitEvent extends GenericEvent
{
	/*
	构造方法 依赖注入 Goods的 数据表
	*/
    protected $order;
    public function __construct(Goods $order)
    {
        $this->order = $order;
    }
    // 返回 数据表的数据
    public function getOrder()
    {
        return $this->order;
    }
}

src/AppBundle/Listener/ContentSubmitListener.php 监听事件的 文件

<?php
/**
 * Created by PhpStorm.
 * AppLication FYC
 * User: FYC
 * Date: 2023/4/7
 * Time: 19:27
 */

namespace AppBundle\Listener;


use AppBundle\Entity\Goods;
use AppBundle\Event\AfterContentSubmitEvent;

class ContentSubmitListener
{
	// 解释:这里面 通过依赖注入的方式,过去了 AfterContentSubmitEvent 
	// 在通过 调用她的方法,获取对应的名字
	// 然后 就是要做的操作,这里举例,通过 strip_tags 取消html输入
    public function onContentSubmit(AfterContentSubmitEvent $event){
        $content = $event->getOrder();
        $messages = $content->getGoodsName();
        $content->setGoodsName(strip_tags($messages)); 
    }
}

订阅事件

src/AppBundle/EventSubscriber/AfterContentSubmitSubscriber.php

namespace AppBundle\EventSubscriber;
use AppBundle\Event\AfterContentSubmitEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class AfterContentSubmitSubscriber implements EventSubscriberInterface
{
    /**
     * @var ContainerInterface
     */
     /*
     *利用 订阅事件 去做查询替换
     */
    private $container;
    public function __construct(ContainerInterface $container = null)
    {
        $this->container = $container;
    }
    public static function getSubscribedEvents(): array
    {
        return [
            KernelEvents::RESPONSE => [
                ['onKernelResponsePre'],
            ],
            AfterContentSubmitEvent::class => 'onAfterContentSubmitEvent',
        ];
    }
    public function onAfterContentSubmitEvent(AfterContentSubmitEvent $event){
        $words = $this->container->getParameter('strip_words'); 
        $content = $event->getOrder();
        $message = $content->getGoodsName();
        $content->setGoodsName($this->stripWords($message,$words));
    }
    public function onKernelResponsePre(FilterResponseEvent $event)
    {
    }
    public function onStoreOrder(AfterContentSubmitEvent $event)
    {
        // ...
    }
    private function stripWords($message, $words)
    {
        foreach($words as $word){
            $message = str_replace($word,'***',$message);
        }
        return $message;
    }
}

插件:

创建插件
app/console plugin:register Example
注册插件
app/console plugin:register Example
注册插件,但不创建数据库:
app/console plugin:register Example --without-database

重写 appBundle

在symfony 目录下的src 里面,有一个文件 :src/AppBundle
这个文件下面的 controller 是所有的方法
那么,在不动用这个文件的基础上,重写这个文件里面的方法
需要在 src 下面创建一个 Bundle , 比如 创建一个 CustomBundle
这个文件, 是要对 AppBundle 进行重写,就需要做一些操作
在根目录下,有一个文件:src/CustomBundle/CustomBundle.php

<?php

namespace CustomBundle;

use Codeages\PluginBundle\System\PluginBase;
use CustomBundle\Biz\CustomServiceProvider;

class CustomBundle extends PluginBase
{
    public function getParent()
    {
        return 'AppBundle';
    }

    public function boot()
    {
        parent::boot();
        $biz = $this->container->get('biz');
        $biz->register(new CustomServiceProvider());
    }

    public function getEnabledExtensions()
    {
        return ['DataTag', 'StatusTemplate', 'DataDict', 'NotificationTemplate'];
    }
}

getParent 方法 下面 return的 就是 哪一个 Bundle ,也就是对哪一个 Bundle 进行重写

重写的规则

例如 :appBundle 文件里面的Controller 里面 有一个这样的文件和方法 —— demo/demo/showAction

解释: 第一个demo:文件名,第二个demo:控制器名字,showAction:方法名字

那么要重写这个 showAction方法:

就要在 CustomBundle 文件下的 Controller 里面,创建一个相同的文件和方法,demo/demo/showAction

理解:例如在 appBunle 下的Controller下 的 demo/demo/showAction 方法里面有一个 echo 123;

CustomBundle 下重写之后,在这个里面的方法 echo 456;
那么页面输出的打印,就会是 456;

记: $this->createService
理解:可以理解为 调用 biz 容器下的 service 服务
例如: return $this->createService(‘S2B2C:S2B2CFacadeService’);
那么 他就是调取了 :
src/Biz/S2B2C/Service/Impl/S2B2CFacadeServiceImpl.php 下的文件
这个文件 就相当于 M层,里面是一些 公共的方法,他就相当于是调用了这个文件

windows 下创建软连接

mklink \j 是命令,topxiaadmin 是添加软连接的文件, …/…/sr… 是创建的软连接的路径

mklink /j topxiaadmin …/…/src/Topxia/AdminBundle/Resources/public/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

付煜晨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值