PHP code example of preflow / folio

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

    

preflow / folio example snippets


return [
    \App\Providers\AppServiceProvider::class,
    \Preflow\Folio\FolioServiceProvider::class,
];



return [
    'path' => '/admin', // or whatever doesn't collide with your app
];

return [
    'cache_path' => sys_get_temp_dir() . '/my-site-folio-cache',
];

'session' => [
    'cookie' => 'site_admin',
    'paths' => ['/folio'], // prefixes, matched per path segment
],

// config/roles.php
return ['staff' => [
    'admin'  => ['*'],
    'editor' => ['folio.dashboard', 'page.view', 'page.edit', 'assets.*'],
]];

// config/folio.php: the app's default guard stays the employers'
return ['path' => '/folio', 'guard' => 'staff'];

namespace App\Folio\Overrides\Content;

use Preflow\Folio\Override\OverridableAction;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

final class Index implements OverridableAction
{
    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        // your custom dashboard
    }
}

$job  = $published->find(Job::class, $id);                        // ?Job, read-only
$jobs = $published->query(Job::class)->where('state', 'open')->get();

use Preflow\Folio\Extension\BulkAction;

#[FolioType('note')]
final class NoteTweaks
{
    #[BulkAction('archive', 'Archive', 'notes.archive', confirm: 'Archive the selected notes?')]
    public function archive(array $records, ServerRequestInterface $request): ?ResponseInterface
    {
        foreach ($records as $record) { /* ... */ }

        return null;
    }
}

return [
    'nav' => ['collapsed' => ['library', 'settings']], // sections closed until a person opens them; [] opens all
];

return ['field_types' => [App\Folio\ColorFieldType::class]];

  return ['revisions' => 20, 'preview_ttl' => 604800];
  

return [...\Preflow\Folio\Console\FolioCommands::all()];

$writer = $container->get(\Preflow\Publishing\RecordWriter::class);
$writer->save($record, new \Preflow\Publishing\SaveOptions(by: $userId, mayPublish: $access->can('article.publish'), loadedVersion: (int) $body['_version']));

try {
    $publisher->saveDraft($record, (int) $body['_version']);
} catch (VersionConflict $e) {
    // show the conflict; $e->current is the version to save against
}

try {
    $publisher->saveDraft($record, (int) $body['_version']);
    $publisher->publish($record, $userId, (int) $record->get('_version'));
} catch (VersionConflict $e) {
    // someone saved in between; nothing was published
}

'assets' => [
    'private_permission' => 'folio.dashboard', // default
    'private_login' => null,                  // optional: '/tools/login'
],

// config/media.php
'private_url' => '/tools/files',
// config/folio.php
'assets' => ['private_permission' => 'members.access', 'private_login' => '/tools/login'],

// app/Folio/Demo/Extension.php
#[FolioExtension(name: 'demo', label: 'Demo', description: 'A screen, a note customization and a setting', icon: 'ui-puzzle', version: '1.0.0')]
#[DeclaresPermission('demo.view', 'See the demo screen')]
#[DeclaresPermission('demo.shout', 'Shout a note')]
final class Extension extends \Preflow\Folio\Extension\Extension
{
    public function settings(): array
    {
        return [
            'greeting' => ['type' => 'string', 'label' => 'Greeting', 'default' => 'Hello from the demo extension'],
        ];
    }
}

public function seed(Container $c): array
{
    $c->get(RoleStore::class)->ensure('staff', 'shop_manager', 'Shop manager', ['shop.view', 'shop.refund']);

    return []; // lines to print, one each
}

// app/Folio/Demo/Screens/Overview.php
#[FolioScreen('demo', 'Demo overview', icon: 'ui-file', permission: 'demo.view')]
final class Overview
{
    public function __construct(private readonly DataManager $data) {}

    #[Get('/')]
    public function index(): ScreenView
    {
        return new ScreenView('@demo/overview.twig', [
            'notes' => $this->data->queryType('note')->get()->items(),
        ]);
    }
}

#[FolioScreen('demo', 'Demo overview', permission: 'demo.view', assets: ['demo.css', '@folio/htmx.js'])]
final class Overview
{
    #[Get('/')]
    public function index(): ScreenView
    {
        // listed after the attribute's assets; a file named in both is 

// app/Folio/Demo/Types/NoteTweaks.php
#[FolioType('note')]
final class NoteTweaks
{
    public function __construct(private readonly DataManager $data) {}

    #[FormPanel('Name length')]
    public function nameLength(DynamicRecord $record): string
    {
        return mb_strlen((string) $record->get('name')) . ' characters';
    }

    #[RecordAction('shout', 'Shout', 'demo.shout', icon: 'ui-check', confirm: 'Upper-case this note\'s name?')]
    public function shout(DynamicRecord $record): null
    {
        $record->set('name', mb_strtoupper((string) $record->get('name')));
        $this->data->saveType($record, false);

        return null;
    }
}

#[FormPanel('Decision', position: 'side')]
public function decision(DynamicRecord $record): ScreenView
{
    return new ScreenView('@members/decision.twig');
}

#[RecordAction('accept', 'Accept', 'members.decide', toolbar: false)]
public function accept(DynamicRecord $record, ServerRequestInterface $request): null { /* ... */ }

#[RecordAction('decline', 'Decline', 'members.decide', toolbar: false)]
public function decline(DynamicRecord $record, ServerRequestInterface $request): null { /* ... */ }

#[ListColumn('State')]
public function state(DynamicRecord $record): Badge
{
    return new Badge('Waiting', 'changed');
}

'greeting' => ['type' => 'string', 'label' => 'Greeting', 'default' => 'Hello from the demo extension'],

$settings = $container->get(\Preflow\Folio\Extension\Settings\ExtensionSettings::class);
$settings->get('demo', 'greeting');          // the stored value, else the default, else null
$settings->get('demo', 'greeting', 'de');    // translatable fields: a locale (falls back like content)
$settings->get('demo', 'greeting', 'de', exact: true); // only de's own value or default, never another locale's

$app->container()->get(\Preflow\Core\Cache\CacheRegistry::class)
    ->register(new \Preflow\Core\Cache\DirectoryCache('images', 'Resized images', $app->basePath('storage/cache/images')));

'providers' => [
    'members' => [
        'class' => Preflow\Auth\Record\RecordUserProvider::class,
        'type' => 'member',
        'email' => 'email', 'password' => 'password_hash', 'roles' => 'roles',  // the defaults
    ],
],
'guards' => [
    'members' => ['class' => Preflow\Auth\SessionGuard::class, 'provider' => 'members'],
],

// config/folio.php
'remotes' => [
    'production' => ['ssh' => 'user@host', 'path' => 'www/site/current', 'port' => 22, 'php' => 'php'],
],

// config/i18n.php
return ['default' => 'en', 'available' => ['en', 'de'], 'fallback' => 'en', 'url_strategy' => 'prefix'];

final class FaqFieldType implements FieldType, ReportsTranslatableTexts
{
    // value: [['question' => ['en' => '…', 'de' => '…'], 'answer' => [...]], ...]
    public function translatableTexts(mixed $value, array $config, Locales $locales): array
    {
        $texts = [];
        foreach ((array) $value as $i => $item) {
            $texts[] = TranslatableText::of("{$i}.question", 'Question ' . ($i + 1), $item['question'] ?? null, $locales);
        }

        return $texts; // reported as "faq.0.question"
    }
}

final class FaqFieldType implements FieldType, ReportsTranslatableTexts, AcceptsTranslatedTexts
{
    // translatableTexts() as above. $texts is path => text, with the paths you reported ("0.question").
    public function withTranslatedTexts(mixed $value, array $texts, string $locale, array $config, Locales $locales): mixed
    {
        $items = (array) $value;
        foreach ($texts as $path => $text) {
            [$i, $key] = explode('.', (string) $path, 2) + [1 => ''];
            if (isset($items[(int) $i][$key])) {
                $map = LocaleValue::map($items[(int) $i][$key], $locales);
                $map[$locale] = $text;
                $items[(int) $i][$key] = $map; // only this language changes
            }
        }

        return $items;
    }
}

// app/Ai/FakeTranslations.php (local only)
final class FakeTranslations implements \Preflow\Ai\AiClientFactory
{
    public function available(#[\SensitiveParameter] array $options): bool
    {
        return true;
    }

    public function create(#[\SensitiveParameter] array $options): \Preflow\Ai\AiClient
    {
        return new \Preflow\Ai\Testing\FakeAiClient([static function (\Preflow\Ai\AiRequest $r) {
            $items = json_decode(substr($r->prompt, (int) strrpos($r->prompt, "\n")), true); // the texts: the prompt's last line

            return \Preflow\Ai\Testing\FakeAiClient::json(array_map(static fn (array $i) => ['n' => $i['n'], 'text' => '[de] ' . $i['text']], $items));
        }]);
    }
}

'translations' => ['editable' => ['review-mail']],
json
"frontend": { "route": "/tools/r/{slug}", "permission": "members.access", "login": "/tools/login" }

*/5 * * * * cd /path/to/app && php preflow publish:scheduled

php preflow folio:extensions:list
php preflow folio:extensions:enable  <name> [--part=<id>]
php preflow folio:extensions:disable <name> [--part=<id>]
php preflow folio:extensions:apply
php preflow folio:extensions:check
php preflow folio:extensions:uninstall <name> [--force]

php preflow folio:pull production --dry-run   # what would change
php preflow folio:pull production             # backup, then replace local content
php preflow folio:pull production --only=data # records only (or --only=media)