我正在使用symfony 3并使用FOSUserBundle。
我希望在网址中没有_locale时自动重定向
例如
它是404
我想要默认。
我的conf.yml
parameters:
locale: en
framework:
#esi: ~
translator: ~ #default
secret: '%secret%'
myroutes:
fos_user_security_login GET|POST ANY ANY /{_locale}/login
fos_user_security_check POST ANY ANY /{_locale}/login_check
fos_user_security_logout GET|POST ANY ANY /{_locale}/logout
fos_user_profile_show GET ANY ANY /{_locale}/profile/
fos_user_profile_edit GET|POST ANY ANY /{_locale}/profile/edit
fos_user_registration_register GET|POST ANY ANY /{_locale}/register/
fos_user_registration_check_email GET ANY ANY /{_locale}/register/check-email
fos_user_registration_confirm GET ANY ANY /{_locale}/register/confirm/{token}
fos_user_registration_confirmed GET ANY ANY /{_locale}/register/confirmed
fos_user_resetting_request GET ANY ANY /{_locale}/resetting/request
fos_user_resetting_send_email POST ANY ANY /{_locale}/resetting/send-email
fos_user_resetting_check_email GET ANY ANY /{_locale}/resetting/check-email
fos_user_resetting_reset GET|POST ANY ANY /{_locale}/resetting/reset/{token}
fos_user_change_password GET|POST ANY ANY /{_locale}/profile/change-password
主页ANY ANY ANY / {_ locale} /
即使我用
注销<a class="btn btn-primary" href="{{ path('fos_user_security_logout') }}">Logout</a>
我有错误,因为我的重定向是/ login
我的security.yml
security:
encoders:
FOS\UserBundle\Model\UserInterface: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
providers:
fos_userbundle:
id: fos_user.user_provider.username
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_token_generator: security.csrf.token_manager
logout:
path: fos_user_security_logout
target: /
anonymous: true
remember_me:
secret: '%secret%'
access_control:
- { path: ^/[a-z]+/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/[a-z]+/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/[a-z]+/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/, role: ROLE_ADMIN }
答案 0 :(得分:0)
在路由配置的开头添加此内容(/app/config/routing.yml
):
# Redirecting the root ("/" to the homepage)
root:
path: /
defaults:
_controller: FrameworkBundle:Redirect:redirect
route: homepage
permanent: true
然后,在您的主页控制器中,确保您的路线包含_locale
参数:
<?php
// src/AppBundle/Controller/Homepage/HomepageController.php
namespace AppBundle\Controller\Homepage;
class HomepageController extends Controller
{
/**
* @Route("/{_locale}",
* name="homepage",
* requirements={
* "_locale": "%app.locales%"
* }
* )
* @Method({"GET", "POST"})
*/
public function homepageAction(Request $request)
{
// your code here
}
}
关于FOSUserBundle
,您应该在路由配置中添加前缀。我假设您已经使用自己的FOSUserBundle
覆盖UserBundle
,这将在文件/src/UserBundle/Resources/config/routing/all.yml
中。然后,您可以选择使用默认的FOSUserBundle
路由还是自定义路由。
# Security
fos_user_security:
# FOSUserBundle default security routes
#resource: "@FOSUserBundle/Resources/config/routing/security.xml"
# UserBundle custom security routes
resource: "@UserBundle/Resources/config/routing/security.yml"
prefix: /{_locale}
requirements:
_locale: '%app.locales%'
# Profile
fos_user_profile:
# FOSUserBundle default profile routes
#resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
# UserBundle custom profile routes
resource: "@UserBundle/Resources/config/routing/profile.yml"
prefix: /{_locale}/profile
requirements:
_locale: '%app.locales%'
# Registration
fos_user_register:
# FOSUserBundle default registration routes
resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
# UserBundle custom registration routes
#resource: "@UserBundle/Resources/config/routing/registration.yml"
prefix: /{_locale}/register
requirements:
_locale: '%app.locales%'
# Resetting Password
fos_user_resetting:
# FOSUserBundle default resetting routes
#resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
# UserBundle custom resetting routes
resource: "@UserBundle/Resources/config/routing/resetting.yml"
prefix: /{_locale}/resetting
requirements:
_locale: '%app.locales%'
当然,我忘了提到您需要在配置中指定参数app.locales
(/app/config/config.yml
):
parameters:
# Locale authorized for routing
app.locales : en|fr|es