PHP code example of apharmony / jsharmony-cms-sdk-php

1. Go to this page and download the library: Download apharmony/jsharmony-cms-sdk-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

apharmony / jsharmony-cms-sdk-php example snippets


new CmsRouter($config)

[
  'content_path' => null,
  //(string) File path to published CMS content files

  'redirect_listing_path' => null,
  //(string) Path to redirect listing JSON file (relative to content_path)

  'default_document' => 'index.html',
  //(string) Default Directory Document

  'strict_url_resolution' => false,
  //(bool) Whether to support URL variations (appending "/" or Default Document)

  'passthru_timeout' => 30,
  //(int) Maximum number of seconds for passthru request

  'cms_clientjs_editor_launcher_path' => '/.jsHarmonyCms/jsHarmonyCmsEditor.js',
  //(string) Path where router will serve the client-side JS script that launches CMS Editor

  'cms_server_urls' => [],
  //Array(string) The CMS Server URLs that will be enabled for Page Editing (set to '*' to enable any remote CMS)
  //  * Used by page.editorScript, and the getEditorScript function
  //  * NOT used by jsHarmonyCmsEditor.js - the launcher instead uses access_keys for validating the remote CMS
]

$cmsRouter = new CmsRouter([ 'cms_server_urls' => ['https://cms.example.com'] ]);

$cmsRouter->config['passthru_timeout'] = 60;

   [
      'onPage' => (function($router, $filename){ }),
      //Function to execute on page route
      //Return value will be passed as return value of "serve" function
      //Default: function($router, $filename){ $router->serveFile($filename); return true; }

      'on301' => (function($router, $url){ }),
      //Function to execute when a 301 redirect is processed
      //Return value will be passed as return value of "serve" function
      //Default: function($router, $url){ $router->redirect301($url); return true; }

      'on302' => (function($router, $url){ }),
      //Function to execute when a 302 redirect is processed
      //Return value will be passed as return value of "serve" function
      //Default: function($router, $url){ $router->redirect302($url); return true; }
      
      'onPassthru' => (function($router, $url){ }),
      //Function to execute when a PASSTHRU redirect is processed
      //Return value will be passed as return value of "serve" function
      //Default: function($router, $url){ $router->passthru($url)->serve(); return true; }

      'on404' => (function($router){ }|null)
      //Function to execute when on 404 / Page Not Found.  Set to null to continue on Page Not Found.
      //Return value will be passed as return value of "serve" function
      //Default: null

      'onError' => (function($router, $err){ }|null) 
      //Function to execute when an unexpected error occurs.  If null, Exception will be thrown instead.
      //Return value will be passed as return value of "serve" function
      //Default: null

      'serveCmsEditorScript' => (bool)
      //Whether the router should serve the CMS Editor Launcher script at config['cms_clientjs_editor_launcher_path']
      //Default: true
   }
   

$cmsRouter->serve();

$cmsRouter->getStandalone();

$cmsRouter->getPlaceholder();

if($cmsRouter->isInEditor()){ /* Perform Operation */ }

   [
      'strictUrlResolution' => (bool), 
      // Whether to try URL variations (adding "/", "/<default_document>")
      // Default: $this->config['strict_url_resolution']

      'variation' => (int)
      // Starting Variation ID
      // Default: 1
   ]
   

$contentPath = $cmsRouter->resolve($targetUrl);

$response = $cmsRouter->route($targetUrl);

$redirect = $cmsRouter->matchRedirect($cmsRedirects);
if($redirect && ($redirect->http_code=='301')){
  $cmsRouter->redirect301($redirect->url);
}

$cmsRedirects = $cmsRouter->getRedirectData();

echo $cmsRouter->getEditorScript();

$pageFilename = $cmsRouter->getPageFileName();
$cmsRouter->serveFile($pageFilename);

$cmsRouter->redirect301('https://example.com');

$cmsRouter->redirect302('https://example.com');

$cmsRouter->passthru('https://example.com')->serve();

$cmsRouter->generate404();

$cmsRouter->generateError('An unexpected error has occurred.');

   [
      'variation' => (int)
      // Starting Variation ID
      // Default: 1
   ]
   

$page = $cmsRouter->getPage($targetUrl);

$page = $cmsRouter->getPageFromFile($filePath);

   [
      'variation' => (int)
      // Starting Variation ID
      // Default: 1
   ]
   

$pageFile = $cmsRouter->getPageFile($targetUrl);

$pageFilename = $cmsRouter->getPageFileName();
$pageContent = $cmsRouter->getFile($pageFilename);

$pageFilename = $cmsRouter->getPageFileName();
$pageData = $cmsRouter->getJsonFile($pageFilename);

echo $page->content->body;
echo $page->content->sidebar;

if($page->properties->showTitle != 'N'){
  echo '<title>'.htmlspecialchars($page->title).'</title>';
}

$page = CmsPage::fromArray([
  'title'=>'Welcome',
  'content'=>['body'=>'Hello World']
]);

new CmsResponse($type)

$response = new CmsResponse('page');

$cmsRouter->passthru('https://example.com')->serve();

new CmsRedirect($http_code, $url)

$redirect = new CmsRedirect('301', 'https://example.com');
js
//Load the CMS Editor in this page
jsHarmonyCmsEditor({ access_keys: ['*****ACCESS_KEY*****'] });