0

I met a few difficult problems when trying to write an API for my blog built with kirby cms .

In a template file called "article.php" in kirby,I want to put the values (e.g. "$page->title()") into an array in order to use the function "json_encode()" to render a json result and then output.

The usage of the kirby method (e.g."$page->title()") is as simple as below:

echo $page->title();

And it can return the title of the current page (e.g."titleOfThisPage").I think(may be wrong,for I am a noob) the value can also be passed to an array in the way like this:

$title = array(
"name" => "title",
"value" => $page->title()
);

But unfortunately,it doesn't work correctly.

Could you please tell (or teach) me what is wrong,and how can I achieve the aim?

Here enclosed the original code:

if($_REQUEST['get'] == "id")
  {
    $json_array = array(
    "status" => "success", 
    "id" => $page->id(), 
   );
   $json_output = json_encode($json_array);
   echo $json_output;
   exit;
  }
  elseif($_REQUEST['get'] == "title")
  {
    $json_array = array( 
    "status" => "success",
    "title" => $page->title(),
   );
   $json_output = json_encode($json_array);
   echo $json_output;
   exit;
  }

This is my first post.I am a Chinese high school student but also a blog and code lover.I find it hard to ask such questions in some websites in China,so I come to Stack Overflow.Please help me with any mistakes(not only programing errors,but also English grammar mistakes for I am also studying English at school) in any of my post, as a noob I would obviously appreciate it!Hoping for an easy solution coming out soon and a long live in Stack Overflow.

Community
  • 1
  • 1
Twikor
  • 1
  • 3

2 Answers2

0

Do you have method $page->title() ??

If you replace this code

"title" => $page->title(),"

to this

"title" => $page->id()," 

in this case

elseif($_REQUEST['get'] == "title")

is it return correct result but using id or not?

Lesiuk Alexey
  • 237
  • 1
  • 2
  • 7
  • The method of "$page->title()" really exists and below is the result returned from "var_dump($page->title())": – Twikor Apr 21 '17 at 11:57
  • object(Field)#107 (3) { ["page"]=> string(54) "work-article/site-building/htaccess-index-page-setting" ["key"]=> string(5) "title" ["value"]=> string(33) ".htaccess文件设置网站首页" } – Twikor Apr 21 '17 at 11:57
  • how you run this request from browser ? – Lesiuk Alexey Apr 21 '17 at 12:03
  • can you add simple php script only with this code and run it - "it work perfectly", I see it !! – Lesiuk Alexey Apr 21 '17 at 12:03
  • It is just a template file,not a part of the kirby program,how can it be difficult to achieve such simple function? – Twikor Apr 21 '17 at 13:12
  • Do you really know about the Kirby CMS? – Twikor Apr 21 '17 at 13:20
  • And how did you think I was telling lies?That is not necessary. – Twikor Apr 21 '17 at 13:29
  • without cms it is work perfect - problem not in this code, maybe need analyze routing of your CMS. Do you add this page to routing path in your CMS? – Lesiuk Alexey Apr 21 '17 at 14:11
  • Yes,and others are alright.But I really do not understand why "$page->id()" returned a string,while "$page->title()" returned an object? – Twikor Apr 22 '17 at 00:06
  • Below is the result returned from "var_dump($page->id())":string(54) "work-article/site-building/htaccess-index-page-setting" – Twikor Apr 22 '17 at 00:08
  • To avoid misunderstanding,I updated the question post,please reread it,thank you very much! – Twikor Apr 22 '17 at 02:17
0

Have found the way out,just adding the type is OK:

  elseif($_REQUEST['get'] == "title")  
  {
    $json_array = array(
     "status" => "success",
     "title" => (string)$page->title(),
   );
   $json_output = json_encode($json_array);
   echo $json_output;
   exit;
  }
Twikor
  • 1
  • 3