Как получить список всех страниц в Rest Api с помощью processwire? - PullRequest
0 голосов
/ 11 февраля 2020

Я новичок в processwire. Я понятия не имею, как создать остальные API. Я хочу показать список всех страниц с помощью остальных API, но не могу получить. Я получил только одну страницу данных при передаче идентификатора страницы в URL. Ниже мой код этот код получить с сайта processwire "https://processwire.com/talk/topic/11806-tutorial-building-a-simple-rest-api-in-processwire/page/2/?tab=comments#comment -172049 ". Пожалуйста, помогите мне создать Rest APi Заранее спасибо

<?php 

require_once "./Rest.php";
$statuscode = 200;
$response = [];
$header = Rest\Header::mimeType('json');
 if($input->urlSegment1 && is_numeric($input->urlSegment1)) { 

    $pageId = $input->urlSegment1;

    if(Rest\Request::is('get')) { 

        // get the page for given Id
        $p = $pages->get($pageId);
        echo "$pageId";

        if($p->id) {
            $pdata = ["id" => $pageId]; // array for storing page data with added page id


            $p->of(false); // set output formatting to false before retrieving page data

            // loop through the page fields and add their names and values to $pdata array
            foreach($p->template->fieldgroup as $field) {
                if($field->type instanceof FieldtypeFieldsetOpen) continue;
                $value = $p->get($field->name); 

                $pdata[$field->name] = $field->type->sleepValue($p, $field, $value);
                 //echo $pdata[$field->name];
                //  echo "krishna";
            }

            // PUT request: update data of existing page
    if(Rest\Request::is('put')) { 

        // get data that was sent from the client in the request body + username and pass for authentication
        $params = Rest\Request::params();

        // verify that this is an authorized request (kept very basic)
        $apiKey = $pages->get("template=api")->key;
        $apiUser = "myapiuser";

        if($params["uname"] != $apiUser || $params["upass"] != $apiKey) {
            // unauthorized request
            $response["error"] = "Authorization failed";
            $statuscode = 401; // Unauthorized (see /site/templates/inc/Rest.php)

        } else {
            // authorized request

            // get the page for given Id
            $p = $pages->get($pageId);

            if($p->id) {
                $p->of(false);

                $p->title = $sanitizer->text($params["title"]);
                $p->name = $sanitizer->pageName($params["name"]);

                $p->save();

                $response["success"] = "Page updated successfully";

            } else {
                // page does not exist
                $response["error"] = "The page does not exist";
                $statuscode = 404; // Not Found (see /site/templates/inc/Rest.php)
            }
        }

    }

            $response = $pdata;

        } else {
            //page does not exist
            $response["error"] = "The page does not exist";
            $statuscode = 404; // Not Found (see /site/templates/inc/Rest.php)
        }

    }

} else {

    if(Rest\Request::is('post')) { 

        // get data that was sent from the client in the request body + username and pass for authentication
        $params = Rest\Request::params();

        // verify that this is an authorized request (kept very basic)
        $apiKey = $pages->get("template=api")->key;
        $apiUser = "myapiuser";

        if($params["uname"] != $apiUser || $params["upass"] != $apiKey) {
            // unauthorized request
            $response["error"] = "Authorization failed";
            $statuscode = 401; // Unauthorized (see /site/templates/inc/Rest.php)

        } else {
            // authorized request

            // create the new page
            $p = new Page();
            $p->template = $sanitizer->text($params["template"]); 
            $p->parent = $pages->get($sanitizer->text($params["parent"])); 
            $p->name = $sanitizer->pageName($params["name"]);
            $p->title = $sanitizer->text($params["title"]); 
            $p->save(); 

            if($p->id) {

                $response["success"] = "Page created successfully";
                $response["url"] = "http://localhost/processwire-master/access/usersy/{$p->id}";

            } else {
                // page does not exist
                $response["error"] = "Something went wrong";
                $statuscode = 404; // just as a dummy. Real error code depends on the type of error.
            }
        }

    }

}

// render the response and body
http_response_code($statuscode);
header($header);
echo json_encode($response);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...