Sebi Posted March 6, 2022 Share Posted March 6, 2022 AppApiPage adds the /page endpoint to the AppApi routes definition. Makes it possible to query pages via the api. This module relies on the base module AppApi, which must be installed before AppApiPage can do its work. Route Description /api/page/ Calls the root page of the page-tree /api/page/42 Will call the page with id=42 /api/page/my/test/page Calls your page with path my/test/page After installing AppApi and AppApiPage, you simply have to add the following code at the top of your ProcessWire-template to provide your page with a custom JSON output: <?php // Check if AppApi is available: if (wire('modules')->isInstalled('AppApi')) { $module = $this->wire('modules')->get('AppApi'); // Check if page was called via AppApi if($module->isApiCall()){ // Output id & name of current page $output = [ 'id' => wire('page')->id, 'name' => wire('page')->name ]; // sendResponse will automatically convert $output to a JSON-string: AppApi::sendResponse(200, $output); } } // Here continue with your HTML-output logic... 6 1 Link to comment Share on other sites More sharing options...
flydev Posted March 7, 2022 Share Posted March 7, 2022 Just to be sure, it's correct to say that if I paste the class you wrote on the other thread and write down the routes, without installing this module it result on the same things ? Link to comment Share on other sites More sharing options...
Sebi Posted March 7, 2022 Author Share Posted March 7, 2022 @flydev ?? Yep, that's right. The module has basically the same code as the PageApiAccess class that I posted in the other thread. I added an improvement for handling the "lang"-GET-param, that enables you to switch to another language in a multilang environment. But everything else is the same code. 2 Link to comment Share on other sites More sharing options...
psy Posted December 3, 2022 Share Posted December 3, 2022 On 3/7/2022 at 9:30 PM, Sebi said: Yep, that's right. The module has basically the same code as the PageApiAccess class that I posted in the other thread. I added an improvement for handling the "lang"-GET-param, that enables you to switch to another language in a multilang environment. But everything else is the same code. Hi @Sebi Would you please give some actual code examples? I've tried all sorts of variations and if I manage to get a response, it's always in the default lang (English), eg: Spoiler <script> // Javscript in page template const testing = async (url) => { let connect = await fetch(url, { method: 'GET', credentials: 'same-origin', mode: 'same-origin', headers: { 'x-api-key': 'xxxxxxxxxxxxxxxxxxxxxxx', } }) let result = await connect.json() return result } // Testing AppApi only // const url ="/api/v1/licki/test" // works. Multilang not tested // const url ="/api/v1/users/41" // works. Multilang not tested // Testing AppApiPage // const url = "/api/page/1/?lang=german" // returns default lang obj {id: 1, name: 'home', title: 'Home', path: '/'}. // title should be German 'Startseite' and path '/de/' // const url = "/api/page/1/de" // fails with {error: 'Method Page::localUrl does not exist or is not callable // in this context', devmessage: {…}} // const url = "/api/page/de/1" // fails with {error: 'Method Page::localUrl does not exist or is not callable // in this context', devmessage: {…}} // const url = "/api/page/home/de" // fails with {error: 'Method Page::localUrl does not exist or is not callable // in this context', devmessage: {…}} // const url = "/api/page/1/?lang=de" // fails with {error: 'Method Page::localUrl does not exist or is not callable // in this context', devmessage: {…}} const url = "/api/page/1/?lang=german" testing(url).then((res) => { console.log(res) } ) </script> Thanks for all the work you've done with AppApi and the add-on modules. Hope you can help. Cheers psy SOLVED (almost!) In _init.php: Spoiler <?php // Check if AppApi is available: if (wire('modules')->isInstalled('AppApi')) { $module = $this->wire('modules')->get('AppApi'); // Check if page was called via AppApi if($module->isApiCall()){ $slug = $user->language->name !== 'default' ? '/' . $pages->get(1)->getLanguageValue($user->language, 'name') : ''; $output = [ 'id' => $page->id, 'name' => $page->name, 'title' => $page->title, 'slug' => \ltrim($slug, '/'), 'path' => $slug . $page->path, 'lang' => $user->language->name, ]; // sendResponse will automatically convert $output to a JSON-string: AppApi::sendResponse(200, $output); } } In my page template javascript: Spoiler <script> const pathArray = window.location.pathname.split('/') console.log(pathArray) const url = `/api/page/1028/?lang=${pathArray[1]}` testing(url).then((res) => { console.log(res) } ) </script> In my AppApi custom class to make it multi-language: Spoiler <?php public static function myFunction($data) { $pages = wire('pages'); $input = wire('input'); $sanitizer = wire('sanitizer'); $languages = wire('languages'); $modules = wire('modules'); $data = AppApiHelper::checkAndSanitizeRequiredParameters($data, ['id|int']); if (!empty($input->get->lang)) { $slug = $sanitizer->pageName($input->get->lang); if ($modules->isInstalled('AppApiPage')) { $langName = AppApiPage::getLanguageCode($slug); if ($langName) $languages->setLanguage($langName); } } $response = new \StdClass(); $page = $pages->get($data->id); if (!$page->id || $page instanceof NullPage) { throw new \Exception('Not found', 404); } $response->id = $page->id; $response->name = $page->name; $response->title = $page->title; return $response; } Everything seems to work fine EXCEPT when using AppApiPage on the home page. The response is still always in the default language. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now