如何安装Symfony2的第三方Bundle

来源:互联网 发布:台湾电视直播软件tv版 编辑:程序博客网 时间:2024/05/16 05:11

Symfony2是一个非常优秀的框架,但它也仅仅只是框架。框架的作用众所周知是为了能够快速开发出代码组织良好的应用程序,而且基于框架开发者可以集中精力进行业务逻辑的开发,而不会将精力浪费在其它细节上面。虽然框架有着很多的好处,但它并不象CMS、Wordpress之类的PHP程序那样是拿来就可以用的,它要实现某种功能是需要开发的。

第三方的Bundle其实部分解决了这个问题,当你需要基于Doctrine的用户和角色时,你无须开发一个用户和角色的Bundle,而只需要安装Symfony2Bundle网站中的UserBundle即可。同样,你需要管理功能,只需安装AdminBundle。同样还有MenuBundle、RestBundle等第三方的Bundle供你使用。那么为什么要使用第三方Bundle而不自行开发呢?原因一来是那句老话不要重复去发明轮子,二来则是与框架一样的,将精力集中在业务逻辑上(这通常是独一无二或无法重用的)。

那么如何将第三方的Bundle安装到Symfony2系统中呢?

一、安装第三方Bundle

首先,找到第三方Bundle,查看它Resources/doc/目录中的index.rst,看看它的Bundle名、下载目录和安装目录。以UserBundle为例,查看其Resources/doc/index.rst文件中的安装语句;

然后,打开Symfony2目录中的deps文件(该文件位于Symfony/根目录下),将安装语句添加在该文件的末尾,如下所示:

  1. [FOSUserBundle] 
  2.     git=git://github.com/FriendsOfSymfony/UserBundle.git 
  3.     target=/bundles/FOS/UserBundle 

最后运行命令行命令(在Symfony2根目录下):

  1. $ php bin/vendors install 
  2. > Installing/Updating symfony 
  3. ... 
  4. > Installing/Updating FOSUserBundle 
  5. Cloning into /var/www/Symfony/vendor//bundles/FOS/UserBundle... 
  6. remote: Counting objects: 7715, done. 
  7. remote: Compressing objects: 100% (2730/2730), done. 
  8. remote: Total 7715 (delta 4942), reused 7285 (delta 4570) 
  9. Receiving objects: 100% (7715/7715), 916.65 KiB | 105 KiB/s, done. 
  10. Resolving deltas: 100% (4942/4942), done. 
  11. HEAD is now at 86bf6fa Updated commands for Symfony changes. Breaks compatibility with beta5 
  12. ... 

这样就已经下载并安装了UserBundle了。

如果你更新第三方的Bundle,你只需要在命令行中输入:

  1. $ php bin/vendors update 
  2. ... 
  3. > Installing/Updating FOSUserBundle 
  4. Cloning into /var/www/Symfony/vendor//bundles/FOS/UserBundle... 
  5. remote: Counting objects: 7720, done. 
  6. remote: Compressing objects: 100% (2733/2733), done. 
  7. remote: Total 7720 (delta 4945), reused 7288 (delta 4571) 
  8. Receiving objects: 100% (7720/7720), 917.08 KiB | 171 KiB/s, done. 
  9. Resolving deltas: 100% (4945/4945), done. 
  10. HEAD is now at affe193 Updated the doc to use the ini syntax for the deps file 
  11. ... 

从上例不难看出UserBundle进行了更新,对文档中的deps文件部分使用了ini语法。

一、添加composer依赖关系

在symfony里,用composer来管理依赖关系

1.找到Bundle的包的名称

在包的README里一般都告诉了我们它的名称,如果没有,可以在https://packagist.org网站里搜索到

2.通过composer来安装Bundle

知道了bundle的包名之后,我们可以通过composer来安装它

1
$ composer require codeguy/upload

codeguy/upload是一个上传文件的bundle,在上一章Symfony2使用第三方库Upload制作图片上传【原创】中我们使用到。

执行上面的指令,composer会给你的项目选择一个最好版本的bundle,把它添加到composer.json中,并将bundle下载到vendor/目录下。如果你想要下载一个指定的版本,在bundle的包名后增加:版本号

转载请注明出处 By 中梦博客 Sun http://blogs.zmit.cn/author/sun

二、注册Bundle

现在,第三方的bundle已经安装到你的symfony项目中了,在vendor/目录下。此时我们需要在app/AppKernel.php里注册安装好的bundle

例如DoctrineFixturesBundle:

1
2
3
4
5
6
7
8
9
10
11
classAppKernel extendsKernel
{
    publicfunction registerBundles()
    {
        $bundlesarray(
            //...在这里注册
            newDoctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),
        );
    }
    //...
}

转载请注明出处 By 中梦博客 Sun http://blogs.zmit.cn/author/sun

三、配置Bundle

有的包需要一些额外的配置在 app/config/config.yml文件里。包的文档会告诉我们关于怎样配置,也可以通过指令来参考包的配置

1
$ app/consoleconfig:dump-reference

例如TwigBundle:

1
$ app/consoleconfig:dump-reference TwigBundle

会得到如下的提示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Default configuration for "TwigBundle"
twig:
    exception_controller:  'twig.controller.exception:showAction'
 
    # Deprecated since 2.6, to be removed in 3.0. Use twig.form_themes instead
    form:
        resources:
 
            # Default:
            - form_div_layout.html.twig
 
            # Example:
            - MyBundle::form.html.twig
    form_themes:
 
        # Default:
        - form_div_layout.html.twig
 
        # Example:
        - MyBundle::form.html.twig
    globals:
 
        # Examples:
        foo:                 "@bar"
        pi:                  3.14
 
        # Prototype
        key:
            id:                   ~
            type:                 ~
            value:                ~
    autoescape:
 
        # Defaults:
        - Symfony\Bundle\TwigBundle\TwigDefaultEscapingStrategy
        - guess
    autoescape_service:   null
    autoescape_service_method:  null
    base_template_class:  ~ # Example: Twig_Template
    cache:                '%kernel.cache_dir%/twig'
    charset:              '%kernel.charset%'
    debug:                '%kernel.debug%'
    strict_variables:     ~
    auto_reload:          ~
    optimizations:        ~
    paths:
 
        # Prototype
        paths:                ~

具体的第三方bundle安装方法,和该bundle的使用方法都可以在它的README文件里查看。


0 0