PHP code example of monoeq / kirby-inertia

1. Go to this page and download the library: Download monoeq/kirby-inertia 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/ */

    

monoeq / kirby-inertia example snippets


<!DOCTYPE html>
<html>
<head>
  <title><?= $site->title() 

return function ($page, $site, $kirby) {
  return Inertia::render(
    $page->intendedTemplate(), 
    $page->toArray()
  );
};

return function ($page, $site, $kirby) {
  return Inertia::render('TemplateName', [
    'title' => $page->title()->value(),
    'date' => $page->date()->value(),
    'thumbnail' => $page->thumbnail()->toFile()->url(),
    'content' => $page->text()->kirbytext()
  ]);
};

return function ($page, $site, $kirby) {
  return Inertia::render('TemplateName', [
    'title' => $page->title()->value(),
    'lazyProp' => function () use ($page) {
      return $page->children()->toArray();
    }
  ]);
};

'monoeq.inertia.shared' => [
  "prop" => "value",
  "beep" => function () {
    return "boop";
  }
]

'monoeq.inertia.shared' => function () {
  return [
    "prop" => "value"
  ];
}

return function ($page, $site, $kirby) {
  
  // Form Handling
  if (kirby()->request()->method() === 'POST') {
    $kirby->impersonate('kirby');
    $page->changeTitle(get('title', ''));
    go($page); // <- Redirect back to GET request for Inertia
  }

  return Inertia::render(
    $page->intendedTemplate(), 
    $page->toArray()
  );
};

return function ($page, $site, $kirby) {
  return Inertia::render(
    $page->intendedTemplate(), 
    $page->toArray(),
    [ 'meta' => 'hello' ]
  );
};

// In your template:
<?= $meta 

return [
  'monoeq.inertia.shared' => [
    'messages' => function () {
      // pull() fetches any session data stored under messages, and then wipes it
      return InertiaSession::pull('messages');
    },
    'errors' => function () {
      // pull() fetches any session data stored under errors, and then wipes it
      return InertiaSession::pull('errors');
    }
  ]
];

return function ($page, $site, $kirby) {
  
  // Form Handling
  if (kirby()->request()->method() === 'POST') {
    try {
      $kirby->impersonate('kirby');
      $page->changeTitle(get('title', ''));
      InertiaSession::append('messages', 'Thank You!');
    } catch (Exception $e) {
      InertiaSession::append('errors', $e->getMessage());
      // or if you want to have named errors
      // InertiaSession::merge('errors', [ 'title' => $e->getMessage() ]);  
    }
    go($page); // <- Redirect back to GET request for Inertia
  }

  return Inertia::render(
    $page->intendedTemplate(), 
    $page->toArray()
  );
};

return [
  'monoeq.inertia.version' => '1.0',
  'monoeq.inertia.shared' => [
    'site' => function () {
      return [
        'title' => site()->title()->value()
      ];
    }
  ]
];

InertiaSession::append('messages', 'beep');
InertiaSession::append('messages', 'boop');
InertiaSession::get('messages'); // => ['beep', 'boop']

InertiaSession::merge('messages', [ 'beep' => 'boop' ]);
InertiaSession::merge('messages', [ 'bleep' => 'bloop' ]);
InertiaSession::get('messages'); // => [ 'beep' => 'boop', 'bleep' => 'bloop' ]