PHP code example of piano / mvc

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

    

piano / mvc example snippets


$this->redirect('/module/controller/action');

$this->redirect(
    '/module/controller/action',
    [
        'firstName' => 'Diogo',
        'lastName' => 'Cavilha',
    ]
);

$id = $this->getParam('id');

$params = $this->getParams();



namespace app\modules\application\controllers;

class IndexController extends Piano\Mvc\Controller
{
    protected function initialize()
    {
        // Do some action before executing any other code of your controller.
    }
}

$this->view->render('view-name');

$this->view->render('view-name', ['name' => 'Diogo']);

// or

$this->view->addVar('name', 'Diogo');
$this->view->addVar('email', '[email protected]');

$this->view->render('view-name');

// or

$this->view->setVars([
    'name' => 'Diogo',
    'email' => '[email protected]',
]);

$this->view->render('view-name');

<p>The user name is:  echo $name 

$this->view->disableLayout(); // disabling
$this->view->disableLayout(true); // disabling
$this->view->disableLayout(false); // enabling

$this->partial('/path/to/file');

$this->partial('/path/to/file', ['title' => 'Piano MVC rocks!']);

$this->view->addCss('/path/to/file1.css');
$this->view->addCss('/path/to/file2.css');
$this->view->addCss('/path/to/file3.css');

$this->view->render('view-name');

// or

$this->view
    ->addCss('/path/to/file1.css')
    ->addCss('/path/to/file2.css')
    ->addCss('/path/to/file3.css')
    ->render('view-name');

$this->view->setCss([
    '/path/to/file1.css',
    '/path/to/file2.css',
    '/path/to/file3.css',
]);

$this->view->render('view-name');

// or

$this->view
    ->setCss([
        '/path/to/file1.css',
        '/path/to/file2.css',
        '/path/to/file3.css',
    ])
    ->render('view-name');


$this->view->addJs('/path/to/file1.js');
$this->view->addJs('/path/to/file2.js');
$this->view->addJs('/path/to/file3.js');

$this->view->render('view-name');

// or

$this->view
    ->addJs('/path/to/file1.js')
    ->addJs('/path/to/file2.js')
    ->addJs('/path/to/file3.js')
    ->render('view-name');

$this->view->setJs([
    '/path/to/file1.js',
    '/path/to/file2.js',
    '/path/to/file3.js',
]);

$this->view->render('view-name');

// or

$this->view
    ->setJs([
        '/path/to/file1.js',
        '/path/to/file2.js',
        '/path/to/file3.js',
    ])
    ->render('view-name');


// Loading the js files
$this->loadJs();

// Loading the css files
$this->loadCss();

protected $table;

protected $model;

protected $pdo;

insert(array $data, array $dataBind)

update(array $data, $where, array $dataBind)

delete($where, array $dataBind = array())

getAll([$configData = null, $order = null, $count = null, $offset = null])

$configData = array(
    'fetchClass' => false,
    'columns' => '*',
    'condition' => 'id = :id',
    'values' => array(
        array(':id', 1, PDO::PARAM_INT)
    )
);

'id ASC, name ASC'

getFirst($configData = null, $order = null)

$configData = array(
    'fetchClass' => false,
    'columns' => '*',
    'condition' => 'id = :id',
    'values' => array(
        array(':id', 1, PDO::PARAM_INT)
    )
);

'id ASC, name ASC'

$pdo = new PDO("mysql:host=host;dbname=db;", 'user', 'pass');

$userDAO = new \app\dataAccess\UserDataAccess($pdo);
$id = $userDAO->insert(
                    array(
                       'name' => ':name',
                       'email' => ':email',
                    ), array(
                       array(':name', 'John Doe', PDO::PARAM_STR),
                       array(':email', '[email protected]', PDO::PARAM_STR),
                    )
                );

$pdo = new PDO("mysql:host=host;dbname=db;", 'user', 'pass');

$userDAO = new \app\dataAccess\UserDataAccess($pdo);
$status = $userDAO->update(
                        array(
                            'name' => ':new_name',
                        ),
                        'name = :where_name',
                        array(
                           array(':new_name', 'New Name', PDO::PARAM_STR),
                           array(':where_name', 'Old Name', PDO::PARAM_STR),
                       )
                    );

$pdo = new PDO("mysql:host=host;dbname=db;", 'user', 'pass');

$userDAO = new \app\dataAccess\UserDataAccess($pdo);
$status = $userDAO->delete(
                        'id = :id',
                        array(
                           array(':id', 2, PDO::PARAM_INT),
                       )
                    );

// or

$userDAO = new \app\dataAccess\UserDataAccess($pdo);
$status = $userDAO->delete('id = 2');


$pdo = new PDO("mysql:host=host;dbname=db;", 'user', 'pass');

$userDAO = new \app\dataAccess\UserDataAccess($pdo);
$users = $userDAO->getAll(
                        array(
                           'columns' => '*',
                           'condition' => 'id = :id',
                           'values' => array(
                               array(':id', 1, PDO::PARAM_INT),
                           )
                        ),
                        'id DESC',
                        10, // show 10 records
                        30 // start showing from the 30th record
                    );

// or

$userDAO = new \app\dataAccess\UserDataAccess($pdo);
$users = $userDAO->getAll();

$pdo = new PDO("mysql:host=host;dbname=db;", 'user', 'pass');

$userDAO = new \app\dataAccess\UserDataAccess($pdo);
$user = $userDAO->getFirst(
                        array(
                           'columns' => '*',
                           'condition' => 'id = :id',
                           'values' => array(
                               array(':id', 1, PDO::PARAM_INT)
                           )
                        )
                    );

// or

$userDAO = new \app\dataAccess\UserDataAccess($pdo);
$user = $userDAO->getFirst();

$this->getApplication()->getModuleName();

$this->getApplication()->getControllerName();

$this->getApplication()->getActionName();

$config = new Piano\Config\Ini('/path/to/config.ini');
$configIni = $config->get();

// getting a simple value

$configIni = $config->get('name');
// If "name" doesn't exist in config file, it will return an empty array.

// getting a section

$configIni = $config->get('section_name');
// If section_name doesn't exist in config file, it will return an empty array.

// The config array must be like this.
$config = [
    'dbAdapter' => 'mysql',
    'dbHost'    => 'localhost',
    'dbName'    => '',
    'dbUser'    => '',
    'dbPass'    => '',
];

$pdo = new Piano\Config\Pdo($config);
$pdo = $pdo->get();

$router = new Piano\Router();

$routes = [
    'default' => [
        'route' => '/',
        'module' => 'application',
        'controller' => 'index',
        'action' => 'index',
    ],
    'contact' => [
        'route' => '/contact',
        'module' => 'application',
        'controller' => 'index',
        'action' => 'contact',
    ],
    'userEdit' => [
        'route' => '/user/:id',
        'module' => 'application',
        'controller' => 'user',
        'action' => 'edit',
        [
            ':id' => '\d+'
        ]
    ],
];

$router->setRoutes($routes);

$router->addRoute('default', '/', [
    'module' => 'application',
    'controller' => 'index',
    'action' => 'index',
]);

$router->addRoute('contact', '/contact', [
    'module' => 'application',
    'controller' => 'index',
    'action' => 'contact',
]);

$router->addRoute('userEdit', '/user/:id', [
    'module' => 'application',
    'controller' => 'index',
    'action' => 'contact',
    [
        ':id' => '\d+'
    ]
]);

$router->enableSearchEngineFriendly(); // Enable
$router->enableSearchEngineFriendly(true); // Enable
$router->enableSearchEngineFriendly(false); // Disable

// It'll return all your routes definitions.
$router->getRoutes();

// It'll return informations from the given route, in this case `myRouteName`
$router->getRoutes('myRouteName');

<a href=" echo $this->url('route_name'); 

<form action=" echo $this->url('contact');