PHP code example of atk4 / ui

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

    

atk4 / ui example snippets




= new \Atk4\Ui\App();
$app->initLayout([\Atk4\Ui\Layout\Centered::class]);

$form = \Atk4\Ui\Form::addTo($app);
$form->addField('email');
$form->onSubmit(function (Form $form) {
    // implement subscribe here

    return $form->jsSuccess('Subscribed ' . $form->entity->get('email') . ' to newsletter.');
});

// decorate anything
$form->buttonSave->set('Subscribe');
$form->buttonSave->icon = 'mail';

// everything renders automatically

$app = new \Atk4\Ui\App(['title' => 'hello world']);
$app->initLayout([\Atk4\Ui\Layout\Admin::class]);
$app->db = \Atk4\Data\Persistence::connect('mysql://user:pass@localhost/atk');

\Atk4\Ui\Crud::addTo($app)
    ->setModel(new User($app->db));

class User extends Model
{
    protected function init(): void
    {
        parent::init();

        $this->addField('name');
        $this->addField('gender', ['enum' => 'female', 'male', 'other']);
        $this->hasMany('Purchases', ['model' => [Purchase::class]]);
    }
}

use \Atk4\Mastercrud\MasterCrud;

// set up $app here

$masterCrud = MasterCrud::addTo($app)
    ->setModel(new User($app->db), [
        'Purchases' => [],
    ]);

$this->addUserAction('archive', function (Model $entity) {
    $this->set('is_archived', true);
    $this->saveAndUnload();
});

$tabs = \Atk4\Ui\Tabs::addTo($app);
\Atk4\Ui\Message::addTo($tabs->addTab('Intro'), ['Other tabs are loaded dynamically!']);

$tabs->addTab('Users', function (\Atk4\Ui\VirtualPage $p) use ($app) {
    // this tab is loaded dynamically, but also contains dynamic component

    \Atk4\Ui\Crud::addTo($p)
        ->setModel(new User($app->db));
});

$tabs->addTab('Settings', function (\Atk4\Ui\VirtualPage $p) use ($app) {
    // second tab contains an AJAX form that stores itself back to DB

    $m = new Settings($app->db);
    $m = $m->load(2);
    \Atk4\Ui\Form::addTo($p)
        ->setModel($m);
});



$app = new \Atk4\Ui\App('My App');
$app->initLayout([\Atk4\Ui\Layout\Admin::class]);
$app->db = \Atk4\Data\Persistence::connect('mysql://user:pass@localhost/yourdb');

class User extends \Atk4\Data\Model
{
    public $table = 'user';

    protected function init(): void
    {
        parent::init();

        $this->addField('name');
        $this->addField('email', ['