window下使用命令行创建symfony项目
2016-04-26 10:01
666 查看
前提:php5.4或更高,配置环境变量
一,创建项目
1.进入命令行模式
2.执行php -r "file_put_contents('symfony', file_get_contents'https://symfony.com/installer'));"
3.移动下载的symfony文件到项目目录
3.1.切换到symfony所在目录
4.执行php symfony,确认下载的文件正常执行
5.执行php symfony new
project_name[空格+版本号]
wait...
命令行执行完成,项目目录下创建了project_name的symfony项目
(命令行中带颜色的字,可以换成其它字符)
二,连接数据库
修改app/config/parameters.yml中的数据库连接参数
三,生成ORM映射
前提:数据库中已存在表结构
执行命令:php bin/console doctrine:mapping:import --force AppBundle yml (文件类型yml可以替换为xml)
提示:执行php bin/console doctrine:mapping:import -h能够列出所有的参数及含义:force覆盖已有的doctrine文件;filter过滤器,可指定要生成的entity等等
在AppBundle/Resources/config/doctrine/下生成对应的数据库表的元数据.
执行:
1) > php bin/console doctrine:mapping:convert annotation ./src 在AppBundle/Entity/下生成实体类.
2) > php bin/console doctrine:generate:entities AppBundle --no-backup 在AppBundle/Entity/下生成实体类,同时生成getter/setter方法
以上两个命令,可根据需要自行选择.
四,插入数据
在DefaultController中实现插入方法:
页面中会显示:
Saved new product with id 1
五,创建新的Bundle
执行命令: php bin/console generate:bundle --namespace=CommonBundle --dir=src --format=annotation --no-interaction
在src目录下创建一个CommonBundle的新Bundle
六,建立CRUD
执行命令: php bin/console generate:doctrine:crud
依次输入->xxBundle:User(Entity实体类名字)->yes->annotation->/user(routing)->yes
Controller目录下会生成UserController,里面已经实现了基础的增册改查功能.
相应模板在app/Resources/view/user
七,模板继承
一般情况下,模板都存放在app/Resources/view中,如果想在各自的Bundle目录下管理模板,模板继承时需要指定模板位置.
{% extends 'CommonBundle::index.html.twig' %}//目录位置src/CommonBundle/Resources/view/index.html.twig
{% extends 'CommonBundle:xx(去掉Controller后的名字):index.html.twig' %}////目录位置src/CommonBundle/Resources/view/xx/index.html.twig
八,日志
Symfnoy框架自带monolog,输出到项目的app/logs下。如果想在程序运行时,把一些需要的信息记录到特定的日志里,这里可以找到答案。记录log可以方便理解程序业务逻辑及流程,也给调试bug提供了查找信息。
在NewBundle的配置service.yml中配置
九,全局常量
1、先在项目app/config中新建一个version.yml文件
在version.yml文件中定义常量
一,创建项目
1.进入命令行模式
2.执行php -r "file_put_contents('symfony', file_get_contents'https://symfony.com/installer'));"
3.移动下载的symfony文件到项目目录
3.1.切换到symfony所在目录
4.执行php symfony,确认下载的文件正常执行
5.执行php symfony new
project_name[空格+版本号]
wait...
命令行执行完成,项目目录下创建了project_name的symfony项目
(命令行中带颜色的字,可以换成其它字符)
二,连接数据库
修改app/config/parameters.yml中的数据库连接参数
parameters: database_host: 127.0.0.1 database_port: null database_name: dbname database_user: root database_password: root mailer_transport: smtp mailer_host: 127.0.0.1 mailer_user: null mailer_password: null secret: a96b1204c31463dc0ac632771c178839c37d5ef7修改app/config/config.yml中doctrine部分:
# Doctrine Configuration doctrine: dbal: driver: pdo_mysql host: "%database_host%" port: "%database_port%" dbname: "%database_name%" user: "%database_user%" password: "%database_password%" charset: UTF8
三,生成ORM映射
前提:数据库中已存在表结构
执行命令:php bin/console doctrine:mapping:import --force AppBundle yml (文件类型yml可以替换为xml)
提示:执行php bin/console doctrine:mapping:import -h能够列出所有的参数及含义:force覆盖已有的doctrine文件;filter过滤器,可指定要生成的entity等等
在AppBundle/Resources/config/doctrine/下生成对应的数据库表的元数据.
执行:
1) > php bin/console doctrine:mapping:convert annotation ./src 在AppBundle/Entity/下生成实体类.
2) > php bin/console doctrine:generate:entities AppBundle --no-backup 在AppBundle/Entity/下生成实体类,同时生成getter/setter方法
以上两个命令,可根据需要自行选择.
四,插入数据
在DefaultController中实现插入方法:
namespace AppBundle\Controller; use AppBundle\Entity\User; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; class DefaultController extends Controller { /** * @Route("/create", name="createUser") */ public function createAction() { $u = new User(); $u->setUserId(10); $u->setStatus(0); $u->setCtime(new \DateTime()); $em = $this->getDoctrine()->getManager(); // tells Doctrine you want to (eventually) save the Product (no queries yet) $em->persist($u); // actually executes the queries (i.e. the INSERT query) $em->flush(); return new Response('Saved new product with id '.$u->getId()); } }在浏览器中输入http://localhost/create
页面中会显示:
Saved new product with id 1
五,创建新的Bundle
执行命令: php bin/console generate:bundle --namespace=CommonBundle --dir=src --format=annotation --no-interaction
在src目录下创建一个CommonBundle的新Bundle
六,建立CRUD
执行命令: php bin/console generate:doctrine:crud
依次输入->xxBundle:User(Entity实体类名字)->yes->annotation->/user(routing)->yes
Controller目录下会生成UserController,里面已经实现了基础的增册改查功能.
相应模板在app/Resources/view/user
七,模板继承
一般情况下,模板都存放在app/Resources/view中,如果想在各自的Bundle目录下管理模板,模板继承时需要指定模板位置.
{% extends 'CommonBundle::index.html.twig' %}//目录位置src/CommonBundle/Resources/view/index.html.twig
{% extends 'CommonBundle:xx(去掉Controller后的名字):index.html.twig' %}////目录位置src/CommonBundle/Resources/view/xx/index.html.twig
八,日志
Symfnoy框架自带monolog,输出到项目的app/logs下。如果想在程序运行时,把一些需要的信息记录到特定的日志里,这里可以找到答案。记录log可以方便理解程序业务逻辑及流程,也给调试bug提供了查找信息。
在NewBundle的配置service.yml中配置
my_logger: class: SymfonyBridgeMonologLogger arguments: [my] calls: - [pushHandler, ["@my_handler"]] my_handler: class: MonologHandlerStreamHandler arguments: [%kernel.logs_dir%/%kernel.environment%.my.log]便可以在control中使用该log,例如
$logger = $this->container->get('my_logger'); $logger->debug(date('Y-m-d H:i:s')); $logger->debug("MY LOG."); $logger->debug("sql=".$sql);然后运行程序,可以看到自定义的log已经生成,即app/logs/prod.my.log
九,全局常量
1、先在项目app/config中新建一个version.yml文件
在version.yml文件中定义常量
parameters: sym_version: 1.02、在config.yml中引入version.yml
imports: - { resource: parameters.ini} - { resource: version.yml}程序中调用
$version = $this->container->getParameter('sym_version');
相关文章推荐
- okhttp是android平台最好的网络库
- cobar实现mysql分片及分片集之内双节点之间的高可用
- Redis 下载
- 按钮打开Activity
- objc非主流代码技巧
- Android NavigationView item点击
- rtmp官方协议详解
- node.js小结
- HTTP的基本原理
- canvas变形方法transform的可视化演示
- Delphi COM编程技术六(DCOM技术)
- laser_scan_merge全部代码分析
- Iptables 使用方法
- SSH密钥认证原理
- Spring JMS 整合 ActiveMQ
- iOS 组件化方案探索
- Jquery - ajax url路径问题
- Huffman编码
- swift基础
- 优化UITableViewCell高度计算的那些事