V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
sanorchid
V2EX  ›  PHP

有人用 slimphp 吗?

  •  
  •   sanorchid · 2017-06-11 22:09:30 +08:00 · 3002 次点击
    这是一个创建于 2508 天前的主题,其中的信息可能已经有所发展或是发生改变。
    菜鸟一个。看该框架的主页 https://www.slimframework.com/,示例代码有一段写法不懂,求解。
    $app->get('/hello/{name}', function (Request $request, Response $response) {
    $name = $request->getAttribute('name');
    $response->getBody()->write("Hello, $name");
    return $response;
    });
    $app->run();

    路由那里,那个匿名函数的两个参数,一个是$request,一个是$response,但是这两个变量前面分别加了的 Request 和 Response 是一个什么写法?
    8 条回复    2017-06-12 12:58:33 +08:00
    gongpeione
        1
    gongpeione  
       2017-06-11 22:15:41 +08:00
    jellybool
        2
    jellybool  
       2017-06-11 22:18:52 +08:00
    我觉得 1 楼的并不是正路

    关键字是:Type Hinting (php 类型约束 )

    个人推荐看这个 http://php.net/manual/en/language.oop5.typehinting.php

    简单理解就是:$request 必须是 Request 的一个实例,$response 同理。
    Jakesoft
        3
    Jakesoft  
       2017-06-11 22:43:46 +08:00
    这跟 slimphp 有什么关系
    Jakesoft
        4
    Jakesoft  
       2017-06-11 22:45:23 +08:00
    这应该是 php 基础语法吧
    imcj
        5
    imcj  
       2017-06-11 23:08:57 +08:00
    @Jakesoft 申明类型,slim 使用了这个特性,这样你可以很容易的在 phpstorm 这样的 ide 里面跳转到 request 的定义。
    Pastsong
        6
    Pastsong  
       2017-06-11 23:46:41 +08:00
    @jellybool 你给的链接还是跳到 1 楼的了 [Facepalm]

    给个中文的 http://php.net/manual/zh/functions.arguments.php#functions.arguments.type-declaration
    sanorchid
        7
    sanorchid  
    OP
       2017-06-12 12:03:59 +08:00
    谢谢大家
    miaotaizi
        8
    miaotaizi  
       2017-06-12 12:58:33 +08:00
    可以先看看这些个东西, 理解里面的一些术语
    https://laravel-china.github.io/php-the-right-way/
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1495 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 17:13 · PVG 01:13 · LAX 10:13 · JFK 13:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.