yii、laravel框架都是基於symfony組件衍生,symfony的強大不用多說。文檔裏有的,很好找的就不寫了
附:
- symfony官網 https://symfony.com/doc/3.4
- yml語法(秒懂的那種)https://www.jianshu.com/p/a8252bf2a63d
- twig語法 https://www.kancloud.cn/yunye/twig-cn/159454
--------------------------
學習筆記(v 3.4.22 附symfony5.0筆記) :
# 展示所有命令
php bin/console
1、bundle
1>創建
php bin/console generate:bundle --namespace=Acme/TestBundle
2>註冊
// app/AppKernel.php
public function registerBundles()
{
$bundles = [
//…,
new BundleNamespace\YourBundle(),
];
if (in_array($this->getEnvironment(), ['dev', 'test'])) {
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
}
return $bundles;
}
2、服務
一旦你啓動一個Symfony應用程序,你的容器已經包含許多服務。這些就像工具*等待你利用它們。
可以通過如下命令查看全部服務:php bin/console debug:container
1>註冊服務
- 在需要的Bundle下新建服務文件夾(名字根據業務邏輯來)
- 在新建的文件夾(Server)下,建立文件(名字根據業務邏輯來)
- 在config文件夾中的services.yml配置文件中註冊服務( 如圖:)
services.yml中配置:
# services.yml
services:
admin.message_generator: # 服務id
class: AdminBundle\Service\MessageGenerator # 文件所在的位置
public: true
autoconfigure: true
autowire: true
2>調用服務
//!!!以下方式必須先use,即:
use namespace\yourServerClass;
//方式1、在controller中 $this->container->get('服務的id')
$doctrine = $this->container->get('doctrine');
//方式2、在controller中 $yourVariate= $this->get(服務的類名::class);
$yourVariate= $this->get(yourServiceClass::class);
//方式3、在服務中獲取其他服務
$doctrine = $this->getContainer()->get('doctrine');
//方式4、在服務的構造中注入其他服務
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RunActionListenter
{
protected $em;
protected $tokenStorage;
public function __construct(TokenStorageInterface $tokenStorage,ContainerInterface $container){
$this->tokenStorage = $tokenStorage;
$this->em = $container->get('doctrine')->getManager();
}
}
3、數據庫
1>常用命令:
//)、通過數據庫生成實體類
php bin/console doctrine:mapping:import --force AppBundle annotation
//AppBundle:bundle名
//annotation:實體類格式 xml,yml, annotation
//)、通過交互式的問題生成實體類
php bin/console doctrine:generate:entity
//)、驗證映射:
php bin/console doctrine:schema:validate
//)、生成geter、seter
php bin/console doctrine:generate:entities AppBundle/Entity --no-backup
//)、根據實體類更新數據庫
php bin/console doctrine:schema:update --force
注意:每次更新完實體後要運行上面的update命令,否則對實體的更改不能及時生效到數據庫!
2>連接多數據庫:
https://www.cnblogs.com/dluf/archive/2013/01/17/2864269.html
3>一些概念:
// 多對一:當前表多條記錄和目標表的一條記錄對應;
//Chatmessages.php
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Role", inversedBy="users")
* @ORM\JoinColumn(name="roles", referencedColumnName="id")
* :
* targetEntity 目標實體類
* inversedBy/mappedBy 要映射到的目標類裏的那個屬性
* (inversedBy一般和Joincolumns並存,處於name外鍵所在的類;mappedBy爲要映射到的類)
* name 當前表中外鍵的名字
* referencedColumnName 參考屬性
*/
private $roles;
// 一對多:
//Disasters.php
/**
* @ORM\OneToMany(targetEntity="Chatmessages", mappedBy="disasterid", cascade={"persist", "remove"})
*
*/
protected $chatmessages;
```
4>其他:
1、時間查詢:
//使用createQuery創建原生查詢
$query = $em->createQuery('SELECT s FROM AppBundle\Entity\Signin s
WHERE s.userid = :uId
AND s.signintime BETWEEN :date AND :endDate');
$query->setParameters(array(
'uId' => $u->getId(),
'date' => date('Y',time()).'-01-01',
'endDate' => date('Y-m-d',time())
));
$res = $query->getResult();
4、文件系統
1>事件對象提供以下方法。
getFile
:獲取上傳的文件。Gaufrette\File
或Symfony\Component\HttpFoundation\File\File
的實例.getRequest
獲取當前請求,包括自定義變量。getResponse
:獲取響應對象以添加自定義返回數據。getType
*獲取當前上載的映射名稱。如果有多個映射和EventListener,則非常有用。getConfig
:獲取映射的配置。
5、http
1>symfony獲取請求的所有參數
適用於GET POST PUT DELETE等所有常用方式,
public function getParams($request){
$query = $request->query->all(); # get
$request = $request->request->all(); # post和其他
return array_merge($query,$params);
}
// 接收文件
$files = $request->files->all(); # 文件
參考自:Symfony\Component\HttpFoundation\Request.php
2>symfony返回一個http狀態碼
return new Response('parameter error',400);
6、獲取配置的參數值
// 在控制器裏可以:
$this->getParameter(參數名);
注:此方法只能獲取parameters.yml裏的,要獲取其他配置文件的參數可以把他們抽離到parameters.yml然後通過%%引用,就可以獲取到了。
--------------------------
未完待續……
最後更新於:2020-4-11