您的位置:首页 > 其它

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中的数据库连接参数

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.0
2、在config.yml中引入version.yml

imports:
- { resource: parameters.ini}
- { resource: version.yml}
程序中调用

$version = $this->container->getParameter('sym_version');
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: