我在当前的Symfony2项目中有一个奇怪的PHP错误:
unexpected T_STRING in /blahblah/Foo/BarBundle/Entity/User.php on line 1
Run Code Online (Sandbox Code Playgroud)
这是一个非常标准的错误,通常与一对不匹配的"或"相关联.
但这里是User.php文件的代码
<?php
namespace Foo\BarBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
* @ORM\Entity(repositoryClass="Foo\BarBundle\Entity\UserRepository")
*/
class User extends \FOS\UserBundle\Entity\User
{
// classic user entity
Run Code Online (Sandbox Code Playgroud)
行<?php是第1行.没有引号,奇怪的是这个错误只出现在我的登台服务器上:在具有代码本地副本的2台开发机器上,它的行为符合预期,没有错误或警告.该文件是正确的,缓存已清空.我认为它可能是一个编码错误但它似乎不是这个.我也想过命名空间问题,但服务器上的PHP版本是正确的(5.3.16)
你知道这个错误可能源于什么,或者我可以搜索哪个方向?提前致谢.
我正在使用Symfony2构建一个站点,它将是一个白标类型的站点,其中多个域映射到同一服务器.因此coolsite.customer1.com和aservice.customer2.com将映射到同一站点,但需要与最终用户显示不同.我已经解决了域名,并将唯一配置作为服务加载.
使用FOS UserBundle设置并运行自定义用户(其中存储了domain_id),注册,登录等工作正常,但domain1的用户也可以登录到domain2.这在FOS UserBundle中是预期的.我需要对捆绑包进行修改,以便它只对在分配给它们的域上的用户进行身份验证.
我创建了一个userProvider,它扩展了FOS中的原始userProvider,并重写了loadUserByUsername方法以检查域.见下文:
use FOS\UserBundle\Security\UserProvider as FOSProvider;
use Symfony\Component\DependencyInjection\ContainerInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use Me\CoreBundle\Models\Core;
class UserProvider extends FOSProvider {
/**
*
* @var ContainerInterface
*/
protected $container;
public function __construct(UserManagerInterface $userManager, ContainerInterface $container) {
parent::__construct($userManager);
$this->container = $container;
}
/**
* {@inheritDoc}
*/
public function loadUserByUsername($username)
{
$core = $this->container->get('me_core');
/* @var $core Core */
$user = $this->findUserBy(array(
'username'=>$username,
'domain_id'=>$core->getDomainMap()->getId(),
));
if (!$user) {
throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
}
return $user;
}
public function findUserBy(array …Run Code Online (Sandbox Code Playgroud) 我正在尝试添加一个Symfony2登录事件监听器,目标是在每个请求上记录用户时设置数据库存储的语言环境,并回退到没有用户登录的默认语言环境.我使用FOSUserBundle,我试图让security.interactive_login事件起作用.我在互联网上找到了很多像这样的代码:http://dev.dbl-a.com/symfony-2-0/how-to-add-a-symfony2-login-event-listener/
我有自己的FOSUserBundle子包和services.yml中的这个实现:
my_user.security.interactive_login_listener:
class: My\UserBundle\EventListener\UserListener
arguments: [@security.context, @doctrine]
tags:
- { name: kernel.event_listener, event: security.interactive_login, method: setLocaleForAuthenticatedUser }
my_user.security.kernel_request_listener:
class: My\UserBundle\EventListener\UserListener
arguments: [@security.context, @doctrine]
tags:
- { name: kernel.event_listener, event: kernel.request, method: setLocaleForUnauthenticatedUser }
Run Code Online (Sandbox Code Playgroud)
问题是,即使在记录时,也永远不会触发security.interactive_login事件.相反,我的setLocaleForUnauthenticatedUser总是被触发.我找到的每个代码示例似乎都很流畅,我的应用程序出了什么问题?
我想做的事情并不难.这是我关于symfony的第一个项目,这真的令人困惑.
我正在使用FOSUSerbundle.我不希望登录和注册下面/login和/registration
所以我做了一个捆绑,这是FOSUSerbundle的孩子......它覆盖了它的树枝.
我有:: base.html.twig,其中包括header.html.twig,我在那里:{% render 'FOSUserBundle:Security:login' %}渲染我的teplate(覆盖了FOS)工作gr8.即使提交后的错误也会在:: base模板下呈现"/"路径.
#Security.yml
form_login:
check_path: /login_check
login_path: /
provider: fos_userbundle
Run Code Online (Sandbox Code Playgroud)
效果很好.
我需要为我的注册做同样的事情.
所以在::base我包含welcome_page.html.twig我编码的地方{% render 'FOSUserBundle:Registration:register' %},我在我的重写模板下面:WelcomePageBundle:Registration:register.html.twig这个:
{% block fos_user_content %}
{% include "FOSUserBundle:Registration:register_content.html.twig" %}
{% endblock fos_user_content %}[/code]
Run Code Online (Sandbox Code Playgroud)
其中还包括我重写的包:WelcomePageBundle:Registration:register_content.html.twig这个:
{% for key, message in app.session.getFlashes() %}
<div class="{{ key }}">
{{ message|trans({}, 'FOSUserBundle') }}
</div>
{% endfor %}
<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" id="register_form">
{{ …Run Code Online (Sandbox Code Playgroud) 所以当我通过生产环境上的fosUserBundle表单注册时,它会向我的Gmail发送一封电子邮件,但电子邮件中没有确认链接,只有这个
registration.email.message
Run Code Online (Sandbox Code Playgroud)
在电子邮件的标题和正文中,有人知道为什么吗?
我想知道是否有用户登录后调用函数.
这是我想要调用的代码:
$point = $this->container->get('process_points');
$point->ProcessPoints(1 , $this->container);
Run Code Online (Sandbox Code Playgroud) 我试图让只有用户ROLE_USER.
我在我的控制器中有这个:
$query = $this->getDoctrine()->getEntityManager()
->createQuery(
'SELECT u FROM StageUserBundle:User u WHERE u.roles LIKE :role'
)->setParameter('role', '%"ROLE_USER"%'
);
$users = $query->getResult();
Run Code Online (Sandbox Code Playgroud)
它合作ROLE_ADMIN而不是,ROLE_USER但我只需要展示User.getRoles()== "ROLE_USER".
我试图结合FOSUserBundle和HWIOAuthBundle之后的文章,如https://gist.github.com/danvbe/4476697.但是,我不希望自动注册OAuth2经过身份验证的新用户:应由用户提供其他信息.
期望的结果
我想为注册用户提供以下信息:
现在,当用户通过Facebook进行身份验证并且用户尚不存在时,我希望注册表单填写缺失的信息(显示名称和个人资料图片).只有在此之后,才应创建新的FOSUser.在大多数教程中,个人资料图片和电子邮件地址等字段会自动填充Facebook信息.这并不总是令人满意也不可能.
此外,在创建用户之前,请考虑接受协议条款和您希望显示的规则.
可能的方法
我认为,解决方案是创建一种新的AnonymousToken,即OAuthenticatedToken,它保存相关的OAuth2信息但不计入身份验证.然后,使所有页面检查此类身份验证,并让其他页面重定向到OAuth-registration-page.然而,这对我来说似乎是一个不必要的复杂解决方案.
另一个解决方案可能是从头开始编写代码,而不是使用提到的两个bundle.我真的希望这不是必要的.
问:如何在登录流程的其余部分插入注册完成代码?
(我很想分享一些代码,但由于这是我需要帮助的概念,我没有太多要展示的.)
编辑:解决方案
继Derick的adivce之后,我得到了这样的基础知识:
自定义用户提供程序保存信息(遗憾的是,无法访问原始令牌,因此我无法在注册后登录用户):
class UserProvider extends FOSUBUserProvider {
protected $session;
public function __construct(Session $session, UserManagerInterface $userManager, array $properties) {
$this->session = $session;
parent::__construct( $userManager, $properties );
}
public function loadUserByOAuthUserResponse(UserResponseInterface $response)
{
try {
return parent::loadUserByOAuthUserResponse($response);
}
catch ( AccountNotLinkedException $e ) {
$this->session->set( 'oauth.resource', $response->getResourceOwner()->getName() );
$this->session->set( 'oauth.id', $response->getResponse()['id'] );
throw $e;
}
}
}
Run Code Online (Sandbox Code Playgroud)
定制失败处理程序
<?php
// OAuthFailureHandler.php
class OAuthFailureHandler …Run Code Online (Sandbox Code Playgroud) 我试图安装sonata admin bundle来管理我的用户.
我使用FOS用户包.
我已经说明了,但是出了什么问题,我找不到.
我有错误:
无法自动确定基本路由名称,请在C:\ Users\Alexandre\hubiC\www\questionnaire\app/config中baseRouteName为管理类定义默认值UserBundle\Admin\UserAdmin.(从"C:\ Users\Alexandre\hubiC\www\questionnaire\app/config\routing.yml"导入).
在我的服务中,我有:
services:
sonata.admin.user:
class: UserBundle\Admin\UserAdmin
tags:
- { name: sonata.admin, manager_type: orm, group: "Content", label: "User" }
arguments:
- ~
- UserBundle\Entity\User
- ~
calls:
- [ setTranslationDomain, [UserBundle]]
Run Code Online (Sandbox Code Playgroud)
在我的配置中:
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
- { resource: @UserBundle/Resources/config/admin.yml }
sonata_block:
default_contexts: [cms]
blocks:
# Enable the SonataAdminBundle block
sonata.admin.block.admin_list:
contexts: [admin]
# Your other blocks …Run Code Online (Sandbox Code Playgroud) 在我个人的Symfony 3.2项目(https://github.com/pc-magas/photoalbum)上因为我想获得一个Json而不是基于http://www.webtipblog.com/adding-an-ajax-login的重定向-form-to-a-symfony-project /我做了以下身份验证管理器:
<?php
namespace AppBundle\Security;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
class AuthenticationHandler implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface
{
private $router;
private $session;
/**
* Constructor
*
* @author Joe Sexton <joe@webtipblog.com>
* @param RouterInterface $router
* @param Session $session
*/
public function __construct( RouterInterface $router, Session $session )
{
$this->router = $router;
$this->session = $session;
}
/**
* onAuthenticationSuccess
* …Run Code Online (Sandbox Code Playgroud) fosuserbundle ×10
symfony ×10
php ×5
doctrine ×1
email ×1
events ×1
login ×1
orm ×1
registration ×1
security ×1
sonata-admin ×1
swiftmailer ×1
symfony-2.1 ×1
syntax-error ×1
twig ×1