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/