PHP code example of selikhovleonid / nadir

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

    

selikhovleonid / nadir example snippets


array(
    // The path map of the application components
    'componentsRootMap' => array(
        'models'      => '/models',
        'controllers' => '/controllers',
        'views'       => '/views/views',
        'layouts'     => '/views/layouts',
        'snippets'    => '/views/snippets',
        'images'      => '/web/assets/imgs',
        'js'          => '/web/js',
        'css'         => '/web/css'
    ),
    // The default name of the layout
    'defaultLayout'     => 'main',
    // The routing table that contains the correspondence between the request URN
    // and the Controller-Action pair
    'routeMap'          => array(
        'cli'    => array(
            '--test' => array(
                'ctrl' => array('Cli', 'actionTest'),
            ),
        ),
        'get'    => array(
            '/'  => array(
                'ctrl' => array('Test', 'actionDefault'),
                'auth' => false,
            ),
            '.*' => array(
                'ctrl' => array('System', 'actionPage404'),
                'auth' => false,
            ),
        ),
        'post'   => array(
            '.*' => array(
                'ctrl' => array('System', 'actionPage404'),
                'auth' => false,
            ),
        ),
        'put'    => array(
            '.*' => array(
                'ctrl' => array('System', 'actionPage404'),
                'auth' => false,
            ),
        ),
        'delete' => array(
            '.*' => array(
                'ctrl' => array('System', 'actionPage404'),
                'auth' => false,
            ),
        ),
    ),
);

\nadir\core\AppHelper::getInstance()->getConfig('configName')

namespace controllers;

use nadir\core\AbstractWebCtrl;

class Test extends AbstractWebCtrl
{

    public function actionDefault()
    {
        // ...
        $this->getView()->foo  = 'foo';
        $this->getView()->bar  = array(42, 'bar');
        $this->getView()->setVariables(array(
            'baz'  => 'baz',
            'qux'  => 'qux',
            'quux' => 'quux',
        ));
        // ...
        $this->render();
    }
}

namespace controllers;

use nadir\core\AbstractCliCtrl;

class Cli extends AbstractCliCtrl
{

    public function actionTest(array $aArgs)
    {
        if (!empty($aArgs)) {
            $this->printInfo('The test cli action was called with args: '
                .implode(', ', $aArgs).'.');
        } else {
            $this->printError(new \Exception('The test cli action was called without args.'));
        }
    }
}

namespace controllers;

use nadir\core\AbstractWebCtrl;

class Test extends AbstractWebCtrl
{

    public function actionDefault()
    {
        // ...
        $this->setView('test', 'default');
        $this->setLayout('main');
        $this->getLayout()->isUserOnline = false;
        $this->getView()->foo            = 'foo';
        $this->getView()->bar            = array(42, 'bar');
        // ...
        $this->render();
    }
}

namespace controllers;

use nadir\core\AbstractWebCtrl;

class Test extends AbstractWebCtrl
{

    public function actionDefault()
    {
        // ...
        $this->setView('test', 'default');
        $this->setLayout('main');
        $this->getView()->addSnippet('topbar');
        $this->getView()
            ->getSnippet('topbar')
            ->isUserOnline               = false;
        $this->getView()->foo            = 'foo';
        $this->getView()->bar            = array(42, 'bar');
        // ...
        $this->render();
    }
}

namespace models;

use extensions\core\AbstractModel;

class Test extends AbstractModel
{

    public function readDefault()
    {
        // Dummy mode
        return array(
            'foo' => 'bar',
            'bar' => array(42, 'baz'),
        );
    }
}

namespace controllers;

use nadir\core\AbstractWebCtrl;

class Test extends AbstractWebCtrl
{

    public function actionDefault()
    {
        $this->getView()->addSnippet('topbar');
        $this->getView()
            ->getSnippet('topbar')
            ->isUserOnline     = false;
        $oModel                = new \models\Test();
        $aData                 = $oModel->readDefault();
        $this->getView()->foo  = $aData['foo'];
        $this->getView()->bar  = $aData['bar'];
        $this->render();
    }
}

namespace extensions\core;

use nadir\core\Request;
use nadir\core\AppHelper;

class Auth extends AbstractAuth
{
    protected $request     = null;
    protected $routeConfig = null;
    protected $error       = null;

    public function __construct(Request $oRequest)
    {
        $this->request     = $oRequest;
        $this->routeConfig = AppHelper::getInstance()->getRouteConfig();
    }

    protected function checkCookies(array $aCookies)
    {
        // Put your code here...
    }

    public function run()
    {
        if (!isset($this->routeConfig['auth'])) {
            throw new \Exception("Undefined option 'auth' for the current route.");
        }
        $mCookies = $this->request->getAllCookies();
        $this->checkCookies(!is_null($mCookies) ? $mCookies : array());
    }

    public function isValid()
    {
        return is_null($this->error);
    }

    public function onFail()
    {
        // Put your code here...
    }
}

'routeMap'          => array(
    // ...
    'get'    => array(
        '/'  => array(
            'ctrl'  => array('Test', 'actionDefault'),
            'roles' => array('admin', 'manager'),
            'auth'  => true,
        ),
        // ...
        '.*' => array(
            'ctrl'  => array('System', 'actionPage404'),
            'roles' => array('admin', 'manager', 'user'),
            'auth'  => true,
        ),
    ),
    // ...
),

namespace controllers;

use nadir\core\AbstractWebCtrl;
use nadir\core\validator\Validator;

class Test extends AbstractWebCtrl
{

    public function actionDefault()
    {
        $aData      = array(
            'foo'  => 'fooValue',
            'bar'  => 'barValue',
            'baz'  => -42,
            'qux'  => false,
            'quux' => array(
                'quuux' => 'quuuxValue',
            ),
        );
        $oValidator = new Validator($aData);
        $oValidator->setItems(array(
            array(
                array('foo', 'bar'),
                '               'integer'  => true,
                    'float'    => false,
                    'positive' => false,
                    'value'    => array('max' => -1),
                )
            ),
            array(
                'qux',
                'boolean',
                array('isTrue' => false),
            ),
            array(
                'quux',
                'array',
                array(
                    'assoc'  => true,
                    'length' => array('equal' => 1),
                ),
            ),
        ));
        if ($oValidator->run()->isValid()) {
            $this->renderJson(array(
                'result' => 'ok',
                'errors' => array(),
            ));
        } else {
            $this->renderJson(array(
                'result' => 'fail',
                'errors' => $oValidator->getErrors(),
            ));
        }
    }
}

php cli.php --test --foo=bar

<!-- ... -->
<div>
    <h1><?= $this->foo; 

<h1>User <?= $this->isUserOnline ? 'online' : 'offline'; 

<!-- ... -->
<div>
     $this->getSnippet('topbar')->render();