, (*1)
Yard: ReactJS Component in PHP
This library allows you to write ReactJS Component in PHP Class., (*2)
It works by creating React Component on-the-fly based on javascript generated by PHP., (*3)
Vanilla PHP Installation
You can install Yard using composer:, (*4)
composer require plansys\yard
Follow these steps to use yard:, (*5)
- Create
my-project
directory
- Open terminal and execute
composer require plansys\yard
in that directory
- Create
pages
, tmp
and redux
directory in my-project
directory
- Make sure
/tmp
directory is writeable by web server process (chmod 755
into this directory)
- Create
base.php
and index.php
file
After those steps, your directory structure should look like this:, (*6)
my-project
โโ tmp (writable)
โโ pages
โโ redux
โโ vendors
โโ ...
โโ yard
โโ base (copy to /bases/default)
โโ src
โโ ...
โโ ...
โโ base.php
โโ index.php
, (*7)
Put this code into base.php
file:
```php
<?php, (*8)
$host = str_replace("index.php", "", strtok($_SERVER["REQUEST_URI"], '?'));
return [
'name' => 'PLANSYS',
'offline' => false,
'settings' => null,
'host' => $host,
'modules' => [
'' => [
'dir'=> dirname(FILE) . '/pages',
'url' => $host . '/pages',
'redux' => dirname(FILE) . '/redux'
]
],
'dir' => [
'dir'=> dirname(FILE) . '/pages',
'base' => dirname(FILE) . '/vendor/plansys/yard/base/build',
'cache' => dirname(FILE) . '/tmp',
'root' => dirname(FILE)
],
'url' => [
'base' => $host . '/vendor/plansys/yard/base/build',
'cache' => $host . '/tmp/[file]',
'page' => $host . '/index.php?p=[page]',
]
];
```, (*9)
And put this code into index.php
file:, (*10)
```php
<?php, (*11)
require("vendor/autoload.php");, (*12)
$base = new \Yard\Base(dirname(FILE) . DIRECTORY_SEPARATOR . "base.php");
$yard = new \Yard\Renderer($base);, (*13)
$parr = explode('...', @$_GET['p']);
$modearr = count($parr) > 1 ? explode(".", $parr[1]) : [''];
$mode = count($modearr) > 1 ? $modearr[1] : $modearr[0];, (*14)
if ($mode == 'css') {
header('Content-type: text/css');
} else if (in_array($mode, ['js', 'jsdev', 'sw'])) {
header('Content-type: text/javascript');
}
$page = isset($_GET['p']) ? $_GET['p'] : 'builder:Index';
echo $yard->render($page);, (*15)
```, (*16)
Then open index.php
in your browser., (*17)
Icons made by Freepik from www.flaticon.com is licensed by CC 3.0 BY, (*18)