如何Location在Express中设置响应HTTP标头?我试过这个,但它不起作用:
Controller.prototype.create = function(prop, res) {
var inst = new this.model(prop);
inst.save(function(err) {
if (err) {
res.send(500, err);
}
res.location = '/customers/' + inst._id;
res.send(201, null);
});
};
Run Code Online (Sandbox Code Playgroud)
此代码将新文档持久保存到MongoDB中,并在竞争时设置位置并发送201响应.得到此回复,没有Location设置标头:
HTTP/1.1 201 Created
X-Powered-By: Express
Content-Length: 0
Date: Mon, 18 Feb 2013 19:08:41 GMT
Connection: keep-alive
Run Code Online (Sandbox Code Playgroud)
编辑:作为uʍopǝpısdn(酷尼克顺丰)回答,res.setHeader工作.但为什么res.location不呢?
我想我对Symfony以及bundle如何工作有很好的理解.
然而,我从来没有找到如何解决一个简单的问题:制作一个可重复使用的捆绑包,提供数据,如表格/主义实体预先填充(即)世界上所有国家名称,意大利所有省份,英格兰的税率历史和等等.
当然,目的是提供依赖于此数据源的表单,服务和控制器,而无需跨项目复制和粘贴表和实体.
你会怎么做?
数据夹具IMHO不是一个选项,因为一个明显的原因:你将在它运行时清除你的数据库.
从静态数据源(json,YAML)读取并执行插入/更新的自定义命令?
我正在使用带有Roboto 2.0字体的Materialise CSS.字体在Chrome 43和Firefox 37中看起来很糟糕.令人惊讶的是它看起来非常好(完全结果):

其他字体(如Lato和Open Sans)以及我的工作计算机也是如此(相同的视频卡和操作系统,如果有关系).我的设置是否有问题(Windows 7 x64 + NVIDIA GTX 780 1920x1080显示屏)?
在我的系统上,大约86000个SQLite插入占用了20分钟,意味着每秒约70次插入.我必须做数百万,我怎么能加快它?在SQLiteConnection对象上为每一行调用Open()和Close()可能会降低性能?交易能帮忙吗?
单线的典型插入方法:
public int InsertResultItem(string runTag, int topicId,
string documentNumber, int rank, double score)
{
// Apre la connessione e imposta il comando
connection.Open();
command.CommandText = "INSERT OR IGNORE INTO Result "
+ "(RunTag, TopicId, DocumentNumber, Rank, Score) " +
"VALUES (@RunTag, @TopicId, @DocumentNumber, @Rank, @Score)";
// Imposta i parametri
command.Parameters.AddWithValue("@RunTag", runTag);
command.Parameters.AddWithValue("@TopicId", topicId);
command.Parameters.AddWithValue("@DocumentNumber", documentNumber);
command.Parameters.AddWithValue("@Rank", rank);
command.Parameters.AddWithValue("@Score", score);
// Ottieni il risultato e chiudi la connessione
int retval = command.ExecuteNonQuery();
connection.Close();
return retval;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,插入非常简单.
我怎样才能将一个额外的参数传递给array_map回调?在我的例子中,我想将$smsPattern(作为第二个参数,在当前元素之后$featureNames)传递给array_map具有$getLimit闭包的函数:
$features = $usage->getSubscription()->getUser()->getRoles();
// SMS regular expression in the form of ROLE_SEND_SMS_X
$smsPattern = '/^ROLE_SEND_SMS_(?P<l>\d+)$/i';
// Function to get roles names and X from a role name
$getNames = function($r) { return trim($r->getRole()); };
$getLimit = function($name, $pattern) {
if(preg_match($pattern, $name, $m)) return $m['l'];
};
// Get roles names and their limits ignoring null values with array_filter
$featuresNames = array_map($getNames, $features);
$smsLimits = array_filter(array_map($getLimit, $featureNames, $smsPattern));
Run Code Online (Sandbox Code Playgroud)
有了这段代码,我得到了一个奇怪的警告: …
这IteratorAggregate是一个创建外部迭代器的接口:
class myData implements IteratorAggregate
{
public $property1 = "Public property one";
public $property2 = "Public property two";
public $property3 = "Public property three";
public function __construct()
{
$this->property4 = "last property";
}
public function getIterator()
{
return new ArrayIterator($this);
}
}
$obj = new myData;
Run Code Online (Sandbox Code Playgroud)
并且您将能够使用foreach以下方式遍历对象:
foreach($obj as $key => $value) {
var_dump($key, $value);
echo "\n";
}
Run Code Online (Sandbox Code Playgroud)
虽然Iterator是外部迭代器或对象的接口,可以在内部迭代:
class myIterator implements Iterator
{
private $position …Run Code Online (Sandbox Code Playgroud) 按照标题,我创建了一个非常简单的测试工具.我想用内置的webserver(PHP 5.4)测试它,但似乎不可能.
php -S localhost:80 /path/to/myphar.php
Run Code Online (Sandbox Code Playgroud)
结果:空白页面(在phar前端控制器中index.php正在进行一些HTML输出).
php -S localhost:80 -t /path/to/folder_with_index_and_phar
Run Code Online (Sandbox Code Playgroud)
使用加载器脚本:
<?php
require 'phar://myphar.php'
Run Code Online (Sandbox Code Playgroud)
再次生成一个空白页面.
这甚至是可能的还是我必须使用Apache?
编辑:一年后我再次需要这个.这是正在发生的事情(来自答案):
php -S localhost:80 -t /path/to/app.phar
php -S localhost:80 -t /path/to/app.phar
Run Code Online (Sandbox Code Playgroud)
导致错误:/ path/to/app.phar不是目录.
而:
php -S localhost:80 -t .
http://localost/app.phar/foo/bar
Run Code Online (Sandbox Code Playgroud)
工作,但我不需要app.phar "前缀"!
使用Apache并将目录索引设置为app.phar按预期工作.
我的路线(slug包含破折号!):
region:
pattern: /regione/{slug}-{id}
defaults: { _controller: SWAItaliaInCifreBundle:Default:region }
Run Code Online (Sandbox Code Playgroud)
在Twig模板中:
{% for r in regions %}
<a href='{{ path('region', { 'slug':r.slug, 'id':r.id }) }}'>{{ r.name }}</a>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
我收到关于正则表达式匹配的错误.问题:为什么Symfony2不允许在URL中使用破折号?如何指定我的路线包含短划线(并且它完全没问题)?
在渲染模板期间抛出异常("参数"slug"for route"区域"必须匹配"[^/ - ] +?"(给出"valle-d-aosta-vallee-d-aoste"). ")
my_bundle:
algorithm: blowfish # One of 'md5', 'blowfish', 'sha256', 'sha512'
Run Code Online (Sandbox Code Playgroud)
此配置由此配置树完成:
// Algorithms and constants to check
$algorithms = array(
'md5' => 'CRYPT_MD5',
'blowfish' => 'CRYPT_BLOWFISH',
'sha256' => 'CRYPT_SHA256',
'sha512' => 'CRYPT_SHA512',
);
$rootNode
->children()
->scalarNode('algorithm')
->isRequired()
->beforeNormalization()
->ifString()
->then(function($v) { return strtolower($v); })
->end()
->validate()
->ifNotInArray(array_keys($algorithms))
->thenInvalid('invalid algorithm.')
->end()
->validate()
->ifTrue(function($v) use($algorithms) {
return 1 != @constant($algorithms[$v]);
})
->thenInvalid('algorithm %s is not supported by this system.')
->end()
->end()
->end();
Run Code Online (Sandbox Code Playgroud)
由于每个算法都需要不同的参数,如何根据所选算法动态添加它们作为根节点的子节点?
例如,如果算法是"blowfish",那么应该有一个名为"cost"的标量节点,而如果"sha512"是一个标量节点"rounds",每个节点都有不同的验证规则.
编辑:我真正需要的是弄清楚当前的算法(如何处理$rootNode?)而不是调用:
$rootNode->append($this->getBlowfishParamsNode());
$rootNode->append($this->getSha256ParamsNode());
$rootNode->append($this->getSha512ParamsNode());
Run Code Online (Sandbox Code Playgroud)
编辑 …
[ 关于收集的文档 ]当嵌入表单(集合类型)可以为每个项目指定验证组时,基于当前项目?它似乎不适用于ATM.
的TaskType形式添加的标签的集合:
// src/Acme/TaskBundle/Form/Type/TaskType.php
// ...
public function buildForm(FormBuilderInterface $builder, array $options)
{
// ...
$builder->add('tags', 'collection', array(
// ...
'by_reference' => false,
));
}
Run Code Online (Sandbox Code Playgroud)
例如,我们有两个标签(标签1和标签2),并使用"添加"按钮(通过JavaScript)添加新标签:
-----------
| add tag |
-----------
- tag 1 (existing)
- tag 2 (added clicking the "add tag" button)
Run Code Online (Sandbox Code Playgroud)
标签1应该针对组进行验证Default,Edit而标签2 Default仅针对组进行验证.
TagType 定义动态验证组的表单根据基础数据,如果tag是new,则获取Defaultgroup(如果存在Default)Creategroup:
// ...
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'validation_groups' => function (FormInterface $form) {
$tag …Run Code Online (Sandbox Code Playgroud) php ×4
symfony ×4
arrays ×2
doctrine-orm ×2
.net ×1
array-map ×1
bundle ×1
c# ×1
callback ×1
closures ×1
css ×1
express ×1
firefox ×1
fonts ×1
http-headers ×1
insert ×1
iterator ×1
materialize ×1
node.js ×1
performance ×1
phar ×1
php-5.4 ×1
regex ×1
routes ×1
routing ×1
sqlite ×1
symfony-2.3 ×1
traversal ×1