是否可以仅为XHR请求限制Symfony 2路由?我想声明只能通过AJAX访问的路由.
我不想在每个特定于AJAX的操作中添加一些额外的行:
if ($request->isXmlHttpRequest()) {
// do something
} else {
// do something else
}
Run Code Online (Sandbox Code Playgroud)
我想定义:
为了避开上述条件.
我试图在另一个服务中使用日志记录服务,以便解决该服务的麻烦.
我的config.yml看起来像这样:
services:
userbundle_service:
class: Main\UserBundle\Controller\UserBundleService
arguments: [@security.context]
log_handler:
class: %monolog.handler.stream.class%
arguments: [ %kernel.logs_dir%/%kernel.environment%.jini.log ]
logger:
class: %monolog.logger.class%
arguments: [ jini ]
calls: [ [pushHandler, [@log_handler]] ]
Run Code Online (Sandbox Code Playgroud)
这在控制器等中运行良好.但是当我在其他服务中使用它时,我没有得到任何结果.
有小费吗?
在symfony中创建路由并且想要有一条路径时的常用解决方案
/{username}
Run Code Online (Sandbox Code Playgroud)
这样它就不会与/ login或/ info之类的其他路由冲突,只是将该路由作为您的routing.yml文件中的最后一条路由.由于所有其他路由优先,因此避免了这种冲突.但是,如果将路径定义为控制器中的注释,如何才能执行此操作?在这种情况下,有没有办法指定这条路线的顺序?
可能有一个自定义存储库与Symfony 2和Doctrine 2中的实体无关吗?我想在其中添加一些不适合其他存储库的本机SQL(它可能指的是抽象或实体层次结构).
如何$this->getDoctrine()->getRepositoty(/* ??? */)
更换控制器代码?
在MongoDB 4.2版copydb
及其copyDatabase
包装器中,已弃用。MongoDB手册建议我们现在应该使用mongodump
和mongorestore
。但是我使用PHP MongoDB驱动程序从PHP调用了copy命令,而dump和restore命令是需要从命令行运行的命令,并且没有任何PHP等效项。现在如何使用PHP复制数据库?
在处理SSL连接并将其作为HTTP发送给您的EC2负载均衡器后面时,确保在Symfony2中使用HTTPS的干净的Symfony方法是什么.
如果您执行requires_channel之类的操作,则会在初始更改为HTTPS后获得重定向循环,因为在Web服务器本身上您仍然在执行HTTP.该网址确实显示了HTTPS,并且亚马逊也提供了X-FORWARDED-PROTO标头.
是否有一种干净的方式让symfony2处理这种情况?
我正在关注Symfony 2网站上如何覆盖Bundle页面的任何部分.这是有趣的:
您可以通过在app/config/config.yml中设置将包含服务类名的参数设置为您自己的类.这当然只有在类名定义为包含服务的包的服务配置中的参数时才可能.
所以我看了,/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Resources/config
我发现这session.xml
是定义%session.class%
参数,所以应该很容易扩展Symfony Session
类,如:
namespace Acme\HelloBundle\Component\HttpFoundation;
use Symfony\Component\HttpFoundation\Session;
class ExtendedSession extends Session
{
public function setSuccessFlashText($text, array params = array())
{
parent::setFlash('success', $this->getTranslator()->trans($text, $params);
}
}
Run Code Online (Sandbox Code Playgroud)
我还没测试过这个.但是我怎么能用request
特殊服务呢?我想添加一些方便的快捷方式,以使我的代码更容易阅读.
我在services.xml
文件中找到了这个:
<!--
If you want to change the Request class, modify the code in
your front controller (app.php) so that it passes an instance of
YourRequestClass to the Kernel.
This service definition only defines the scope of …
Run Code Online (Sandbox Code Playgroud) 我正在使用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) 我有一个值存储在我的parameters.ini文件中,我需要在我的模型的prepersist方法中访问它.
通常我使用$this->container->getParameter('value');
,但容器在实体中不可用.
有没有办法在实体类中获取参数?
PS该值是我在prepersist期间从中提取信息的服务的API密钥.最佳做法是将密钥/密码保存在parameters.ini中
我正在构建一个只提供json/xml数据的RESTful应用程序,我选择了Silex,因为我已经知道(有点)Symfony 2,因为它很小,我不需要Twig等...
没有模型,只有使用Doctrine dbal的普通旧SQL查询和序列化程序.无论如何,我应该验证POST/PUT请求.如何在不使用表单组件和模型的情况下完成此操作?
我的意思是POST数据是一个数组.我可以验证它(添加约束)以及如何?
编辑:好的,现在我发现了一个有趣的库,这是尊重/验证.如果需要,它还使用sf约束.我最终得到了这样的东西(早期的代码:P),如果没有更好的东西,我将使用它:
$v = $app['validation.respect'];
$userConstraints = array(
'last' => $v::noWhitespace()->length(null, 255),
'email' => $v::email()->length(null, 255),
'mobile' => $v::regex('/^\+\d+$/'),
'birthday' => $v::date('d-m-Y')->max(date('d-m-Y')),
);
// Generic function for request keys intersection
$converter = function(array $input, array $allowed)
{
return array_intersect_key($input, array_flip($allowed));
};
// Convert POST params into an assoc. array where keys are only those allowed
$userConverter = function($fields, Request $request) use($converter) {
$allowed = array('last', 'email', 'mobile', 'birthday');
return $converter($request->request->all(), $allowed);
};
// …
Run Code Online (Sandbox Code Playgroud) symfony ×9
php ×3
ajax ×1
amazon-ec2 ×1
doctrine ×1
doctrine-orm ×1
model ×1
mongodb ×1
parameters ×1
rest ×1
silex ×1
symfony-2.1 ×1