fulgurio/social-network-bundle

Symfony FulgurioSocialNetworkBundle

安装: 207

依赖: 0

建议者: 0

安全: 0

星标: 8

关注者: 3

分支: 6

公开问题: 1

类型:symfony-bundle

dev-master 2015-08-04 15:45 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:17:58 UTC


README

SocialNetworkBundle 为您的 symfony 2 项目提供创建社交网络的支持。

先决条件

翻译

如果您希望使用本包提供的默认文本,请确保在配置中启用了翻译器。

# app/config/config.yml

framework:
    translator: ~

有关翻译的更多信息,请参阅 Symfony 文档

安装

此包使用 FOSUser。此包的配置也包含在此文档中。

安装是一个简单的 3 步过程

  1. 下载 FulgurioSocialNetworkBundle
  2. 配置自动加载器
  3. 启用包
  4. 配置应用的 security.yml
  5. 配置 FOSUserBundle
  6. 配置包
  7. 导入 FulgurioSocialNetworkBundle 路由
  8. 更新数据库模式

第 1 步:下载 FulgurioSocialNetworkBundle

最终,FulgurioSocialNetworkBundle 文件应下载到 vendor/bundles/Fulgurio/SocialNetworkBundle 目录。

这可以通过几种方式完成,具体取决于您的偏好。第一种方法是标准的 Symfony2 方法。

使用 vendors 脚本

在您的 deps 文件中添加以下行

[FOSUserBundle]
    git=git://github.com/FriendsOfSymfony/FOSUserBundle.git
    target=bundles/FOS/UserBundle
    version=1.2.0

[Stof-DoctrineExtensionsBundle]
    git=http://github.com/stof/StofDoctrineExtensionsBundle.git
    version=origin/1.0.x
    target=/bundles/Stof/DoctrineExtensionsBundle

[gedmo-doctrine-extensions]
    git=http://github.com/Atlantic18/DoctrineExtensions.git
    version=origin/2.2.x

[knp-components]
    git=http://github.com/KnpLabs/knp-components.git
    version=v1.1

[KnpPaginatorBundle]
    git=http://github.com/KnpLabs/KnpPaginatorBundle.git
    target=bundles/Knp/Bundle/PaginatorBundle
    version=v2.2

[FulgurioSocialNetworkBundle]
    git=git://github.com/Fulgurio/SocialNetworkBundle.git
    target=bundles/Fulgurio/SocialNetworkBundle

现在,运行 vendors 脚本来下载包

$ php bin/vendors install

使用子模块

如果您更愿意使用 git 子模块,请运行以下命令

$ git submodule add git://github.com/FriendsOfSymfony/FOSUserBundle.git vendor/bundles/FOS/UserBundle
$ git submodule add git://github.com/stof/StofDoctrineExtensionsBundle.git vendor/bundles/Stof/DoctrineExtensionsBundle
$ git submodule add git://github.com/Atlantic18/DoctrineExtensions.git vendor
$ git submodule add git://github.com/KnpLabs/knp-components.git vendor
$ git submodule add git://github.com/KnpLabs/KnpPaginatorBundle.git vendor/bundles/Knp/Bundle/PaginatorBundle
$ git submodule add git://github.com/Fulgurio/SocialNetworkBundle.git vendor/bundles/Fulgurio/SocialNetworkBundle
$ git submodule update --init

第 2 步:配置自动加载器

将命名空间添加到您的自动加载器中

<?php
// app/autoload.php

$loader->registerNamespaces(array(
    // ...
    'FOS' => __DIR__.'/../vendor/bundles',
    'Stof'             => __DIR__.'/../vendor/bundles',
    'Gedmo'            => __DIR__.'/../vendor/gedmo-doctrine-extensions/lib',
    'Knp\\Component'   => __DIR__.'/../vendor/knp-components/src',
    'Knp\\Bundle'      => __DIR__.'/../vendor/bundles',
    'Fulgurio' => __DIR__.'/../vendor/bundles',
));

第 3 步:启用包

最后,在 kernel 中启用包

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new FOS\UserBundle\FOSUserBundle(),
        new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
        new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(),
        new Fulgurio\SocialNetworkBundle\FulgurioSocialNetworkBundle(),
    );
}

第 4 步:配置应用的 security.yml

为了让 Symfony 的安全组件使用 FOSUserBundle,您必须在 security.yml 文件中指定。这是包含您应用程序安全基本配置的 security.yml 文件。

以下是使用 FOSUserBundle 在您的应用程序中所需的配置示例

# app/config/security.yml
security:
    providers:
        fos_userbundle:
            id: fos_user.user_manager

    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    firewalls:
        main:
            pattern: ^/
            form_login:
                provider:      fos_userbundle
                csrf_provider: form.csrf_provider
                remember_me:   true
            logout:       true
            anonymous:    true
            remember_me:
                key:      "%secret%"
                lifetime: 31536000 # 365 days, in seconds
                path:     /
                domain:   ~

    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/friends/, role: IS_AUTHENTICATED_FULLY }
        - { path: ^/messenger/, role: IS_AUTHENTICATED_FULLY }
        - { path: ^/admin/, role: ROLE_ADMIN }

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

providers 部分,您通过别名 fos_userbundle 使包的用户提供者服务可用。包的用户提供者服务的 ID 是 fos_user.user_manager

接下来,看看 firewalls 部分。这里我们声明了一个名为 main 的防火墙。通过指定 form_login,您告诉 Symfony2 框架,任何请求到该防火墙导致用户需要认证自己的情况,用户将被重定向到一个表单,可以输入其凭据。因此,您指定的用户提供者是我们之前声明过的,作为防火墙在认证过程中使用的提供者。

第 5 步:配置 FOSUserBundle

现在您已正确配置了应用程序的 security.yml 以与 FOSUserBundle 一起使用,下一步是配置包以适应您应用程序的具体需求。

根据您使用的数据存储类型,将以下配置添加到您的 config.yml 文件中。

# app/config/config.yml
fos_user:
    db_driver:     orm
    firewall_name: main
    user_class:    Fulgurio\SocialNetworkBundle\Entity\User
    registration:
        form:
            type:  fulgurio_social_network_registration_type
    resetting:
        form:
            type:  fulgurio_social_network_resetting_type
    profile:
        form:
            type:  fulgurio_social_network_profile_type

stof_doctrine_extensions:
    orm:
        default:
            timestampable: true
            sluggable: true

第 6 步:配置包

现在配置包,只需设置联系邮箱(它是包发送邮件的 "From:" 邮箱)

# app/config/config.yml
fulgurio_social_network:
    contact:
        admin:
            email:
                address: contact@example.com
                sender_name: Contact
    avatar:
        admin:
            remove:
                email:
                    address: contact@example.com

第 7 步:导入 FulgurioSocialNetworkBundle 路由

现在您已激活并配置了包,剩下的只是导入 FulgurioSocialNetworkBundle 路由文件。

通过导入路由文件,您将获得登录、创建用户等内容的现成页面。

在 YAML 中

# app/config/routing.yml
fos_user_security:
    resource: "@FulgurioSocialNetworkBundle/Resources/config/routing.yml"

或如果您更喜欢 XML

<!-- app/config/routing.xml -->
<import resource="@FulgurioSocialNetworkBundle/Resources/config/routing.yml"/>

步骤 8:更新数据库模式

现在配置包已经设置好,你需要做的最后一件事是更新数据库模式,因为你已经添加了一个新实体,即你在步骤 4 中创建的 User 类。

对于 ORM,请运行以下命令。

$ php app/console doctrine:schema:update --force

完成 FulgurioSocialNetworkBundle 的基本安装和配置后,你就可以学习更多关于该包的高级功能和用法了。