我正在构建一个只提供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) 假设我在 MongoDB 中执行以下操作列表
insert使用新文档运行命令find对插入文档的集合运行命令我知道在事务之外,在提交事务之前,第三步中完成的插入将不可见,但是在事务内呢,find第四步中的运行会看到这个新文档吗?
我正在尝试将Behat带到https安全项目,并且在启动curl请求时mink失败.
Scenario: Loggin in # features/debt.feature:6
Given I am on "/" # FeatureContext::visit()
[curl] 51: SSL: certificate subject name 'ubuntu' does not match target host name 'wizard' [url] https://wizard/admin/dev.php/ [info] array (
'url' => 'https://wizard/admin/dev.php/',
'content_type' => NULL,
'http_code' => 0,
'header_size' => 0,
'request_size' => 0,
'filetime' => -1,
'ssl_verify_result' => 1,
'redirect_count' => 0,
'total_time' => 0.061943,
'namelookup_time' => 0.000234,
'connect_time' => 0.000344,
'pretransfer_time' => 0,
'size_upload' => 0,
'size_download' => 0,
'speed_download' => 0,
'speed_upload' => 0, …Run Code Online (Sandbox Code Playgroud) 所以我目前正在尝试使用Symfony2和Doctrine进行简单的搜索.与此类似的东西:http://docs.doctrine-project.org/projects/doctrine1/en/latest/en/manual/searching.html
我目前有以下YAML文件设置来生成我的实体.它class Style作为一个类正确地生成我的实体.
...\Style:
type: entity
table: styles
id:
id:
type: integer
generator:
strategy: IDENTITY
actAs:
Searchable:
fields: [title]
batchUpdates: true
fields:
title:
type: string
length: 150
unique: true
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我正在尝试基于字符串在该表上运行搜索.
public function searchAction($pattern)
{
$repository = $this->getDoctrine()->getRepository('..:Style');
$search = $repository->search($pattern);
return $this->outputize($search);
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试执行代码时,我得到以下异常.
Undefined method 'search'. The method name must start with either findBy or findOneBy!
Run Code Online (Sandbox Code Playgroud)
我是否正确地生成了我的实体,或者是否有一些我明显缺失的东西?
另一方面,当我看到我Entity/Style.php生成后,没有明确的方法->search(),这个函数应该由Symfony在这里生成吗?
当我编辑用户的帖子时,我使用以下网址:
../post/edit/3 //If the id of the post is 3 for example
Run Code Online (Sandbox Code Playgroud)
例如/post/edit/5,为避免用户故意修改网址,我使用以下逻辑确保用户在没有权限时不编辑帖子:
if (//user is allowed to edit post){
//edit post
}
else {
throw new AccessDeniedException('You do not have the permission to edit this post');
}
Run Code Online (Sandbox Code Playgroud)
这是编辑帖子时使用的一般方法吗?有没有办法做一些更干净的东西,以便用户不能使用网址中的帖子的ID?
我越是想到它,我就越发现我从未在一个关注安全性的网站上看到过像这样的网址中的id.所以,我同意我们仍然可以使用id并检查用户是否可以显示/看到此ID,但用户仍然可以做太多.散列id不是更好,允许我们使用任何可用的算法生成新的加密ID:
<?php
echo hash('md5', 'id_to_edit');
?>
Run Code Online (Sandbox Code Playgroud)
在URL中保护id的标准方法是什么?一般来说,在网址中显示id这样的信息是个好主意吗?
我希望能够将调试消息写入日志文件.这样做的正确方法是什么?绝对没有我能在那里找到的文件.我知道Monolog包已被提及,但它没有写任何东西.请建议.
在自动加载文件时,Symfony 2.0 Autoloader期望它可以处理的库遵循PSR0或PEAR标准.如果你有一个不遵循这两个标准中的任何一个的旧库(在我的例子中,类文件被命名为name.class.php),你将如何处理这些库的自动加载?
在Symfony 2.1这个很容易为作曲家的支持classmaps,可以加载这个类型库,但你怎么会在做到这一点Symfony 2.0.x?
我有index.html.twig和base.html.twig在同一个目录文件夹中..我有以下scipts
index.html.twig
{% extends('base.html.twig') %}
{% block body %}
helo body
{{ parent() }}
{% endblock %}
{% block footer %}
This footer
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
base.html.twig
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>{% block title %}Welcome!{% endblock %}</title>
{% block stylesheets %}{% endblock %}
<link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
</head>
<body>
{% block body %}The body block{% endblock %}
{% block sidebar %}The body sidebar{% endblock %}
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
它返回一个错误"无法找到模板"base.html.twig"in"FacebookBundle:Default:index.html.twig".我也注意到有些人在模板名称前面使用过::为什么会这样我该如何解决这个问题?
我将包含git存储库的目录复制到另一个位置.我对一些文件进行了一些更改并进行了提交.然后我试图推动这些更改但我被告知我不在分支中(似乎移动存储库导致了这一点).所以,认为这是正确的事情,我切换到我的主分支.我的所有变化都消失了.我想,没问题,我会合并最后一次提交,一切都会好的.但我无法在任何地方找到这个提交.当我做git log时,它没有显示.如何找到此提交并合并其更改?
我正在使用Tree doctrine扩展来获取类别树,并且希望拥有以下路由:
/cat/subcat1/subcat2/subcat3
Run Code Online (Sandbox Code Playgroud)
我可以这样定义路线
/{cat}
/{cat}/{subcat}
/{cat}/{subcat}/{subcat2)
etc...
Run Code Online (Sandbox Code Playgroud)
但有没有更优雅和一般的方式来实现这一点?一个可以接受无限数量级别的系统?