创建项目
composer create-project symfony/skeleton:"^4.2" export-service
composer require symfony/orm-pack
获取request
$request = $this->container->get('request_stack')->getCurrentRequest();
$request->request->get('ids');
//https://www.test.com
$request->getSchemeAndHttpHost();
//加入cookie
$response = new Response();
$response->headers->setCookie(new Cookie("access_token", $accessToken, time() + 86000, "/", ".sdsm.net"));
$response->sendHeaders();
//cookie设置过期
$response->headers->setCookie(new Cookie("access_token", null, -1, "/", ".sdsm.net"));
//$response->send();
清除 Symfony 缓存
Symfony程序在初次运行之前会生成一些缓存文件以提升性能(例如把Twig模板和XML/YAML/Annotations的配置信息转换成PHP代码)
php bin/console cache:clear --env=prod
将 Bundle 中 public 下的资源文件映射到 web目录中
php bin/console assets:install --symlink
查看框架的版本号和命令列表
php bin/console -v
创建 controller
php bin/console make:controller test
创建数据库
php bin/console doctrine:database:create
删除数据
php bin/console doctrine:database:drop --force
创建实体类
php bin/console make:entity test
校验元数据
php bin/console doctrine:schema:validate
生成 Getter 和 Setter
php bin/console doctrine:generate:entities namespace
查看事件及监听
php bin/console debug:event-dispatcher
查看所有服务容器
php bin/console debug:container
数据库执行更新
php bin/console doctrine:schema:update --force
自己写命令类,使用自定义的命令
<?php
//eg: php bin/console orders:customer-consume --start_month=1 --end_month=3
namespace App\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
class CustomerConsumeCommand extends ContainerAwareCommand
{
use ContainerAwareTrait;
/** @var Connection */
private $conn;
/**
* 参数配置.
*/
protected function configure()
{
$this->setName('orders:customer-consume')
->setDescription('客户的订单消费统计')
->addArgument('method', InputArgument::OPTIONAL, 'Chouse an action to run.')
// configure an argument / 配置一个参数
->addOption('start_month', null, InputOption::VALUE_REQUIRED, 'The beginning month of the year')
->addOption('end_month', null, InputOption::VALUE_REQUIRED, 'The end month of the year')
// 运行命令时使用 "--help" 选项时的完整命令描述
->setHelp('This command test help');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int|void|null
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$start_month = $input->getOption('start_month');
$end_month = $input->getOption('end_month');
if ($start_month > 12 || $start_month < 1 || !ctype_digit($start_month)) {
$output->writeln('the Argument start_month must between 1 and 12');die;
}
if ($end_month > 12 || $end_month < 1 || !ctype_digit($start_month)) {
$output->writeln('the Argument end_month must between 1 and 12');die;
}
$name = $input->getArgument('method');
if ($name == 'action1') {
$text = $this->myAction1();
}
$kernel = $this->getApplication()->getKernel();
$batchDir = $kernel->getProjectDir();
$this->conn = $this->getContainer()->get('doctrine.dbal.default_connection');
$list = $this->conn->fetchAll($sql);
}
protected function myAction1() {
//绿色文字
$output->writeln('<info>绿色文字</info>');
//黄色文字
$output->writeln('<comment>黄色文字</comment>');
//红色文字
$output->writeln('<fg=red>333</>');
//红色背景白字
$output->writeln('<error>foo</error>');
}
}
一个Command多个命令分支
php bin/console orders:customer-consume action1 --start_month=2