【问题标题】:Equivalent of express app.get("/:users/:names") in yaml for PHP?等效于 PHP 的 yaml 中的 express app.get("/:users/:names")?
【发布时间】:2015-06-20 03:31:15
【问题描述】:

您知道如何在 Node.js/Express/MongoDB 堆栈中完成所有工作

app.get("/") {
  helloWorld()
}

app.get("/:users/:names") {
  script()
}

App Engine 的 app.yaml 是否也是同样的想法?还是我以完全倒退的方式查看此文件。这个文件相当于我的路由文件吧?

handlers:
- url: /.*
  script: helloworld.php
  
- url: /users/names/
  script: getName.php

因此,如果我的应用想要获取 user-253 的姓名,它会选择用户,并使用从该 GET 请求中获取的信息查询姓名?

【问题讨论】:

    标签: javascript php node.js yaml


    【解决方案1】:

    我使用 Symfony 读取 yaml 文件:http://symfony.com/doc/current/components/yaml/yaml_format.html

    代码如下所示:

    $yamlFile = __DIR__ . '/Routes.yaml';
    $routes = yaml::parse(file_get_contents($yamlFile));
    
    // Get routes into an associative array and show the urls.
    $handlers = $routes['handlers'];
    
    echo 'Url: ' . $handlers[0]['url'] . "\n";
    echo 'Script: ' . $handlers[0]['script'] . "\n";
    echo "\n";
    echo 'Url: ' . $handlers[1]['url'] . "\n";
    echo 'Script: ' . $handlers[1]['script'] . "\n";
    
    /*
    Output:
    
    Url: /.*
    Script: helloworld.php
    
    Url: /users/names/
    Script: getName.php
    
    */
    

    注意:您需要按如下方式格式化您的 yaml 文件,否则会抛出此异常:您无法在映射中定义序列项。

    handlers:
        - url: /.*
          script: helloworld.php
    
        - url: /users/names/
          script: getName.php
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      • 1970-01-01
      • 2019-03-28
      • 1970-01-01
      • 1970-01-01
      • 2011-03-13
      • 2010-11-04
      相关资源
      最近更新 更多