Symfony 4 fosuserbundle [英] Symfony 4 fosuserbundle
问题描述
我从 Symfony 4 开始,并且我想使用以下链接安装FosUserBundle: https://symfony.com/doc/master/bundles/FOSUserBundle/index.html
I begin with Symfony 4 and I want to install FosUserBundle with this link : https://symfony.com/doc/master/bundles/FOSUserBundle/index.html
第一:
我的问题是我不知道在哪里可以找到"app/config/config.yml"文件来取消注释转换器并进行配置:
My problem is that I don't know where to find the "app/config/config.yml" file to uncomment the translator and to configure :
fos_user:
db_driver: orm # other valid values are 'mongodb' and 'couchdb'
firewall_name: main
user_class: AppBundle\Entity\User
from_email:
address: "%mailer_user%"
sender_name: "%mailer_user%"
第二个:
我认为我必须在"config/packages/"目录中创建security.yml文件,对吗?
I think that I have to create the security.yml file in "config/packages/" directory, is that right ?
第三:
在哪个文件中添加路线?
And in which file to add the route ?
能帮我吗? :)
推荐答案
我已经解决了以下问题:
I've resolved the problem followed this:
-
使用作曲家下载FOSUserBundle:
download FOSUserBundle using composer:
作曲家需要Friendsofsymfony/用户捆绑〜2.0"
composer require friendsofsymfony/user-bundle "~2.0"
在安装结束时,您将收到以下错误消息:
At the end of the installation you will have the following error message :
必须配置路径"fos_user"的子节点"db_driver".
The child node "db_driver" at path "fos_user" must be configured.
- 创建您的User类 创建src/Entity/User.php作为扩展FOSUserBundle BaseUser类的自定义用户类.
- Create your User class Create src/Entity/User.php as custom user class who extend the FOSUserBundle BaseUser class.
<?php
//src/Entity/User.php
namespace App\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct()
{
parent::__construct();
// your own logic
}
}
- 配置应用程序的security.yml 修改config/packages/security.yaml以设置FOSUserBundle安全性
- Configure your application's security.yml Modify config/packages/security.yaml to setup FOSUserBundle security
security:
encoders:
FOS\UserBundle\Model\UserInterface: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
fos_userbundle:
id: fos_user.user_provider.username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_token_generator: security.csrf.token_manager
logout: true
anonymous: true
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/, role: ROLE_ADMIN }
- 配置FOSUserBundle 为配置FOSUserBundle创建一个新文件config/packages/fos_user.yaml
- Configure the FOSUserBundle Create a new file config/packages/fos_user.yaml for the configuration of FOSUserBundle
fos_user:
db_driver: orm # other valid values are 'mongodb' and 'couchdb'
firewall_name: main
user_class: App\Entity\User
from_email:
address: "vincent@vfac.fr"
sender_name: "vincent@vfac.fr"
更新config/packages/framework.yaml以添加模板配置
Update config/packages/framework.yaml to add templating configuration
framework:
templating:
engines: ['twig', 'php']
- 导入FOSUserBundle路由 创建config/routes/fos_user.yaml
- Import FOSUserBundle routing Create config/routes/fos_user.yaml
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
- 更新您的数据库架构 如果尚未完成,则必须创建数据库
- Update your database schema If not already done, you must create your database
php bin/控制台学说:数据库:创建
php bin/console doctrine:database:create
使用您的User类实体中的信息更新架构
Update the schema with the informations from your User class entity
php bin/控制台学说:schema:update --force
php bin/console doctrine:schema:update --force
此时,已安装所有组件并将其配置为使用Symfony 4中的FOSUserBundle.运行以下命令以检查一切是否正常
At this point, all is installed and configured to use FOSUserBundle in Symfony 4. Run the following command to check if all is ok
作曲家更新
composer update
如果没有任何错误消息,可以进行测试! 您可以运行Web服务器来测试您的应用程序
If you don't have any error message, you can test ! You can run the web server to test your application
php bin/控制台服务器:启动
php bin/console server:start
所有教程此处: https://vfac.fr/blog/how-install-fosuserbundle-with -symfony-4
这篇关于Symfony 4 fosuserbundle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!