我有一个查找页面数据的服务,但是如果找不到该数据,则应该重定向到主页.对于我的生活,我无法弄清楚如何在Sf2中做到这一点.有许多不同的方法可以使用服务和路由器,但似乎都没有.
namespace Acme\SomeBundle\Services;
use Acme\SomeBundle\Entity\Node;
use \Doctrine\ORM\EntityManager;
use \Symfony\Component\HttpKernel\Event\GetResponseEvent;
use \Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use \Symfony\Bundle\FrameworkBundle\Routing\Router;
use \Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\HttpFoundation\RedirectResponse;
class NodeFinder
{
private $em;
private $router;
public function __construct(EntityManager $em, Router $router)
{
$this->em = $em;
$this->router = $router;
}
public function getNode($slug)
{
$node = $this->em->getRepository('SomeBundle:Node')->findOneBy(array('slug' => $slug));
if (!$node) { //if no node found
return $this->router->redirect('homepage', array(), true);
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个登录管理区域,我们强制要求https://.如果用户没有登录,点击路由/管理员应该重定向到登录页面,但我得到一个无限的重定向循环.不知道出了什么问题,这是security.yml:
firewalls:
admin_login:
pattern: ^/admin/secured/login$
security: false
admin_secured_area:
pattern: ^/admin
provider: entity_admin
form_login:
check_path: /admin/secured/login_check
login_path: /admin/secured/login
default_target_path: /admin
logout:
path: /admin/secured/logout
target: /
access_control:
- { path: ^/admin/secured/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
- { path: ^/admin, roles: ROLE_ADMIN, requires_channel: https }
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
我正在为Symfony2设计一个多租户应用程序,其中每个租户都可以拥有一个覆盖默认应用程序模板的主题.因此,主题将具有唯一base.html.twig文件,并且可能包含或不包含覆盖默认模板文件的其他文件.
Symfony2已经检查app/Resources/views了覆盖包模板的模板.但Symfony2假设app/Resources/views只有一组模板可以覆盖默认模板.我想动态检查租户的自定义主题文件夹中的各种覆盖模板,例如:
- 主题:
app/Resources/views/theme1/base.html.twig- 主题:
app/Resources/views/theme2/base.html.twigapp/Resources/views/theme2/SomeBundle/Resources/views/page.html.twig
我不确定在Symfony2中构建它的最佳方法,并在Twig中配置它.我应该将所有不同的主题堆放到app/Resources/views中的文件夹中吗?或者我应该创建一些处理一切的ThemeBundle?谢谢!
我正在尝试使用此验证器https://github.com/1000hz/bootstrap-validator进行客户端检查,然后允许ajax提交整个表单.我在文档中找不到一个示例,并且我当前的代码不起作用,它将值作为GET查询字符串重新加载整个页面.有什么想法吗?谢谢!
$('#send-form').validator().on('submit', function (e) {
if (e.isDefaultPrevented()) {
// handle the invalid form...
} else {
// everything looks good!
$.post( "/post/ajax",
$( "#send-form" ).serialize(),
function( data ) {
$( "#ajax-result" ).html( data );
});
}
});
Run Code Online (Sandbox Code Playgroud) 我正在Symfony2中构建一个多租户应用程序.对于安全的"管理"区域,我有一个自定义实体提供商(请参阅:http://symfony.com/doc/current/cookbook/security/entity_provider.html)
但是,似乎Symfony2仅支持在单个属性上检查实体.
my_entity_provider:
entity:
class: SecurityBundle:User
property: email
Run Code Online (Sandbox Code Playgroud)
但是,在我的应用中,单个用户可以拥有多个具有相同电子邮件地址的帐户.我需要的是在登录时检查租户ID属性.
my_entity_provider:
entity:
class: SecurityBundle:User
property: email, tenantID
Run Code Online (Sandbox Code Playgroud)
我不知道如何在Symfony2中实现这一目标.我在创建新用户时已经能够覆盖loadUsername方法,但是Symfony2安全性中的login_check并没有使用它(这真的很难看).
public function loadUserByUsername($username)
{
/* we create a concatenated string in the User entity to pass both
the email and tenantId values as the "username" */
$user_parts = explode("|", $username);
$q = $this->createQueryBuilder('u')
->where('u.tenantId = :tenantid AND u.email = :email')
->setParameter('tenantID', $user_parts[1])
->setParameter('email', $user_parts[0])
->getQuery();
try {
$user = $q->getSingleResult();
} catch (NoResultException $e) {
throw new UsernameNotFoundException(sprintf('Unable to …Run Code Online (Sandbox Code Playgroud) 嗨,我在Silex路线中有一个变量,我们只允许使用字母数字值
->assert('hash','\w+')
Run Code Online (Sandbox Code Playgroud)
我想在变量中也允许一个点,但我编辑这个正则表达式的努力失败了.非常感谢,谢谢!
您好,在管理系统上工作。我想在运行 @SecurityAssert\UserPassword之前运行验证约束“NotBlank” 。(否则会出现不需要的数据库命中,并且为用户提供两个不同的错误消息)这可能吗?谢谢!
use Symfony\Component\Security\Core\Validator\Constraints as SecurityAssert;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Constraints\NotBlank;
class YourClassName
/**
* @Assert\NotBlank( message = "For security reasons, please enter your current password.")
* @SecurityAssert\UserPassword(
* message = "Wrong value for your current password"
* )
*/
protected $oldPassword;
Run Code Online (Sandbox Code Playgroud) 我正在使用KnpMenuBundle,并希望访问菜单Builder类中的Doctrine Entity Manager.我想检查数据库中可能需要显示在菜单中的一些不同值.我尝试通过构造函数传递EM,但它不起作用.实现这一目标的最佳方法是什么?谢谢!
这是我添加到Builder类的代码:
private $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
Run Code Online (Sandbox Code Playgroud)
这会抛出一个错误:Catchable Fatal Error:参数1传递给XXX\ThemeBundle\Menu\Builder :: __ construct()必须是Doctrine\ORM\EntityManager的实例,没有给出,调用
您好我想将Yahoo BOSS API与Symfony2集成,但Yahoo建议的OAuth代码类似乎不适用于现代PHP框架.
http://oauth.googlecode.com/svn/code/php/OAuth.php
/* Generic exception class
*/
class OAuthException extends Exception {
// pass
}
class OAuthConsumer {
public $key;
public $secret;
function __construct($key, $secret, $callback_url=NULL) {
$this->key = $key;
$this->secret = $secret;
$this->callback_url = $callback_url;
}
function __toString() {
return "OAuthConsumer[key=$this->key,secret=$this->secret]";
}
} [...]
Run Code Online (Sandbox Code Playgroud)
http://developer.yahoo.com/boss/search/boss_api_guide/codeexamples.html#oauth_php
我认为OAuth类有命名空间问题,我需要采取哪些步骤来在Symfony2中实现这个类?