小编gre*_*emo的帖子

如何在Express框架中设置位置响应HTTP标头?

如何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不呢?

http-headers node.js express

18
推荐指数
2
解决办法
2万
查看次数

捆绑包如何提供"默认数据",即Symfony 2中的预填表?

我想我对Symfony以及bundle如何工作有很好的理解.

然而,我从来没有找到如何解决一个简单的问题:制作一个可重复使用的捆绑包,提供数据,如表格/主义实体预先填充(即)世界上所有国家名称,意大利所有省份,英格兰的税率历史和等等.

当然,目的是提供依赖于此数据源的表单,服务和控制器,而无需跨项目复制和粘贴表和实体.

你会怎么做?

数据夹具IMHO不是一个选项,因为一个明显的原因:你将在它运行时清除你的数据库.

从静态数据源(json,YAML)读取并执行插入/更新的自定义命令?

bundle symfony doctrine-orm

17
推荐指数
1
解决办法
3667
查看次数

Roboto来自materializecss奇怪的字体渲染在Chrome,Firefox,OK和IE?

我正在使用带有Roboto 2.0字体的Materialise CSS.字体在Chrome 43和Firefox 37中看起来很糟糕.令人惊讶的是它看起来非常好(完全结果):

在此输入图像描述

其他字体(如Lato和Open Sans)以及我的工作计算机也是如此(相同的视频卡和操作系统,如果有关系).我的设置是否有问题(Windows 7 x64 + NVIDIA GTX 780 1920x1080显示屏)?

css firefox fonts google-chrome materialize

17
推荐指数
3
解决办法
6159
查看次数

SQLite .NET的性能,如何加快速度?

在我的系统上,大约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)

如您所见,插入非常简单.

.net c# sqlite performance insert

16
推荐指数
1
解决办法
2万
查看次数

如何在PHP中向array_map回调传递一个额外的参数?

我怎样才能将一个额外的参数传递给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)

有了这段代码,我得到了一个奇怪的警告: …

php arrays closures callback array-map

16
推荐指数
1
解决办法
1万
查看次数

PHP接口IteratorAggregate vs Iterator?

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)

php arrays iterator traversal

16
推荐指数
4
解决办法
8104
查看次数

如何从内置的PHP 5.4 webserver运行webphar PHP文件?

按照标题,我创建了一个非常简单的测试工具.我想用内置的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按预期工作.

php phar php-5.4

16
推荐指数
3
解决办法
1901
查看次数

Symfony2如何允许在路由正则表达式中使用短划线?

我的路线(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"). ")

regex routing routes symfony

15
推荐指数
2
解决办法
1万
查看次数

如何动态地将部分附加到Symfony 2配置?

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)

编辑 …

symfony

15
推荐指数
1
解决办法
1682
查看次数

在Symfony 2中为集合的每个项目指定不同的验证组?

[ 关于收集的文档 ]当嵌入表单(集合类型)可以为每个项目指定验证组时,基于当前项目?它似乎不适用于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 symfony-forms symfony doctrine-orm symfony-2.3

15
推荐指数
1
解决办法
3550
查看次数