Page data from routes

How can I access the content from a page within the routes?

I’ve got a route like this:

array(
    "pattern"   => "api/v2/openhours/(:any)",
    "method"    => "GET",
    "action"    => function ($attraction) {
        $attractionInfo = page("booking/" . $attraction);
        if (!$attractionInfo) {
            return response::json(array(
                "code"      => "A001",
                "message"   =>  "Attraction does not exist, please check your input."
            ));
        }

        return response::json(array(
            "opening" => "world",
            "host" => $_SERVER["HTTP_HOST"],
            "test" => $attractionInfo->open()
        ));
    }
)

When I check the specific route, I don’t get any response, but if I do eg. print_r() of $attractionInfo->open(), it shows me that I have to specify it to be $attractionInfo->open()->value() for it to get the value stored in it.

This is what I get when I do print_r

Field Object
(
    [page] => booking/prison-island
    [key] => open
    [value] => 11
)

Doesn’t your test already contain the answer? If you use value() in your response array, you should get the desired result?

return response::json(array(
          "opening" => "world",
          "host" => $_SERVER["HTTP_HOST"],
          "test" => $attractionInfo->open()->value()
      ));

Yes, I get the value by using value(), but I weren’t sure if that was correct. Thought maybe it was like when I use site() in templates, I just had to use $attractionInfo->open()or have I been doing it wrong all this time?

Yes, whenever you need a string, using value() is absolutely correct. $attractionInfo->open() returns a field object, not a string.

Ok, thanks for clarifying that :sunglasses: :thumbsup: