Slim路径捕获的变量未定义

时间:2015-05-06 20:06:09

标签: php variables slim

我有以下细长路线,但由于某种原因,$cid变量未被识别。 $cid var应该从下面代码的第一行初始化

:cid

然后在响应数组的path变量中进一步使用它。

"path" => "/customers/".$cid."/vehicles/".$vehicle_id

这是完整的代码

$app->put('/customers/:cid/vehicles/?', function() use($app){

    $body = $app->request->getBody();
    $ds = getDataSource();

    $_PUT = array();
    parse_str($body, $_PUT);

    $vehicle_id = $ds->addVehicle( 
        $_PUT["owner"],
        $_PUT["year"],
        $_PUT["make"],
        $_PUT["model"],
        $_PUT["plates"],
        $_PUT["comments"] );

    if( $vehicle_id != null ){

        $response = array(
            "id" => $vehicle_id,
            "path" => "/customers/".$cid."/vehicles/".$vehicle_id
        );

        respond(200, $response);

    } else {
        $response = array(
            "Message"=> "Unable to add vehicle"
        );
        respond(400, $response);
    }
});

所以我有点困惑,为什么我得到:

Slim Application Error

The application could not run because of the following error:
    Type: ErrorException
    Code: 8
    Message: Undefined variable: cid

任何人都知道为什么会这样,或者如何修复它?

1 个答案:

答案 0 :(得分:0)

这不是我最亮的时刻。

#include <boost/multi_array.hpp> #include <iostream> int main() { using namespace boost; using MA = multi_array<double, 2>; MA ma(extents[12][34]); auto& ma_shape = reinterpret_cast<boost::array<size_t, MA::dimensionality> const&>(*ma.shape()); // demo std::cout << "[" << ma_shape[0] << "][" << ma_shape[1] << "]\n"; // resize MA other; assert(!std::equal(ma_shape.begin(), ma_shape.end(), other.shape())); other.resize(ma_shape); assert(std::equal(ma_shape.begin(), ma_shape.end(), other.shape())); // reshape other.resize(extents[1][12*34]); assert(!std::equal(ma_shape.begin(), ma_shape.end(), other.shape())); other.reshape(ma_shape); assert(std::equal(ma_shape.begin(), ma_shape.end(), other.shape())); }

应该是

$app->put('/customers/:cid/vehicles/?', function() use($app){ $ CID $app->put('/customers/:cid/vehicles/?', function(

必须将var传递给闭包。

DERP。