2017 © Pedro Peláez
 

library mvc

A very simple MVC support and routing.

image

piano/mvc

A very simple MVC support and routing.

  • Wednesday, February 21, 2018
  • by diogocavilha
  • Repository
  • 1 Watchers
  • 5 Stars
  • 209 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 28 Versions
  • 0 % Grown

The README.md

Build Status, (*1)

Piano MVC

This is not a micro/mini/whatever framework nor claims to be., (*2)

It's only a very simple MVC structure., (*3)

WARNING: This has been just like a lab. I don't update this project anymore. If you want to use it, the ball is on your court., (*4)

Creating the project structure

composer create-project piano/mvc:dev-application myProject

Controller

Redirecting

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

Redirecting with variables

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

Getting params by name

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

Getting all params

$params = $this->getParams();

Methods from Piano\Mvc\Controller

initialize()

If you need to create a constructor method for your controller, you can do this by creating a method called initialize() instead of __construct(). The __construct() method is already being used by Piano\Mvc\Controller., (*5)

Example:

view->render('view-name');
```

#### Rendering a view with variables

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

// or

$this->view->addVar('name', 'Diogo');
$this->view->addVar('email', 'email@domain.com');

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

// or

$this->view->setVars([
    'name' => 'Diogo',
    'email' => 'email@domain.com',
]);

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

So, in the view code you can access the variable:

```php

The user name is: </p> , (*6)

Disabling/Enabling the layout

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

Adding a partial

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

Adding a partial with variables

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

Loading CSS files

You can load CSS files on demand. It means you can define what CSS files are gonna be loaded when some specific view is rendered. Better than that, you can do this for each of your views., (*7)

You can use the addCss() method., (*8)

$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');

Or you may want to use the setCss() method., (*9)

$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');

PS: You must call addCss() or setCss() method before calling the render() method. Otherwise it won't work., (*10)

Loading javascript files

You can load javascript files on demand. It means you can define what javascript files are gonna be loaded when some specific view is rendered. Better than that, you can do this for each of your views., (*11)

You can use the addJs() method., (*12)

$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');

Or you may want to use the setJs() method., (*13)

$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');

PS: You must call addJs() or setJs() method before calling the render() method. Otherwise it won't work., (*14)

In order to load these CSS or JS files in your view/layout you can call the loadCss() or loadJs() method., (*15)

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

// Loading the css files
$this->loadCss();
<a href="<?php echo $this->url('route_name'); ?>">Text</a>

Form action must be written by using a pre-defined route

<form action="<?php echo $this->url('contact'); ?>" method="post">
    Name: <input type="text" name="name">
    Email: <input type="text" name="email">
    <input type="submit" name="Send">
</form>

Model

The DataAccessAbstract class.

The Piano\Mvc\DataAccessAbstract abstract class provides us a few methods for handling data by accessing the database., (*16)

Attributes from Piano\Mvc\DataAccessAbstract

protected $table;

Table name for working., (*17)

protected $model;

If not null, it's used for fetching its model. Otherwise, it will use an associative array., (*18)

protected $pdo;

A PHP PDO instance., (*19)


Methods from Piano\Mvc\DataAccessAbstract

insert(array $data, array $dataBind)

Create a record into database., (*20)

Parameters

data, (*21)

Array data to insert into the database table., (*22)

dataBind, (*23)

The bound values., (*24)

Return Values

Returns the last insert id on success or FALSE on failure., (*25)


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

Change a database record., (*26)

Parameters

data, (*27)

Array data to insert into the database table., (*28)

where, (*29)

Where clause., (*30)

dataBind, (*31)

The bound values., (*32)

Return Values

Returns TRUE on success or FALSE on failure., (*33)


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

Delete a record from database., (*34)

Parameters

where, (*35)

Where clause., (*36)

dataBind, (*37)

The bound values., (*38)

Return Values

Returns TRUE on success or FALSE on failure., (*39)


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

Perform a query in order to return all database records., (*40)

Parameters

configData, (*41)

The query configuration., (*42)

Example:, (*43)

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

order, (*44)

Like SQL ORDER., (*45)

Example:, (*46)

'id ASC, name ASC'

count, (*47)

Integer value used to make the query return a specific set of rows., (*48)

offset, (*49)

Integer value used to make the query return a specific set of rows., (*50)

PS: When both count and offset are used, the query to be executed has LIMIT offset, count, (*51)

Return Values

  • When fetchClass parameter is true or omitted, it returns an array of model objects.
  • When fetchClass parameter is false, it returns an associative array.

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

Perform a query in order to return all database records., (*52)

Parameters

configData, (*53)

The query configuration., (*54)

Example:, (*55)

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

order, (*56)

Like SQL ORDER., (*57)

Example:, (*58)

'id ASC, name ASC'

Return Values

  • When fetchClass parameter is true or omitted, it returns a model object.
  • When fetchClass parameter is false, it returns an associative array.

Examples:

insert()
$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', 'john@domain.com', PDO::PARAM_STR),
                    )
                );
update()
$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),
                       )
                    );
delete()
$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');

getAll()
$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();
getFirst()
$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();

Application

Getting module name

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

Getting controller name

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

Getting action name

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

Class Piano\Config\Ini

The Piano\Config\Ini is a ini file parser. You must pass the ini path as an argument to its constructor., (*59)

Sample
$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.

Class Piano\Config\Pdo

The Piano\Config\Pdo provide us an easier way to configure a \Pdo connection. You must pass an config array as an argument to its constructor., (*60)

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

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

Class Piano\Router

The Piano\Router is a route parser. It helps you to define the application's routes., (*61)

Sample
$router = new Piano\Router();

You're able to define all routes as an array or by using the Piano\Router object notation., (*62)

Defining routes as array., (*63)

$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);

Defining routes as object., (*64)

$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+'
    ]
]);

You can also enable/disable SEF. (It's useful to disable it when developing) It can be done by calling the method enableSearchEngineFriendly, (*65)

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

If you want to get all routes or a particular one, you could do like this:, (*66)

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

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

The Versions

21/02 2018

dev-master

9999999-dev

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

25/02 2017

dev-2.0-beta

dev-2.0-beta

A very simple MVC support and routing.

  Sources   Download

The Requires

 

The Development Requires

by Diogo Alexsander Cavilha

14/02 2017

dev-application

dev-application

Piano MVC application project.

  Sources   Download

The Requires

 

The Development Requires

by Diogo Alexsander Cavilha

02/09 2016

dev-project

dev-project

Piano MVC application project.

  Sources   Download

The Requires

 

The Development Requires

by Diogo Alexsander Cavilha

14/02 2016

1.6.3.1

1.6.3.1

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

09/02 2016

1.6.3

1.6.3.0

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

03/11 2015

1.6.1.1

1.6.1.1

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

23/10 2015

1.6.1

1.6.1.0

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

23/10 2015

1.6.0

1.6.0.0

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

18/10 2015

1.5.6

1.5.6.0

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

11/10 2015

1.5.5

1.5.5.0

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

11/10 2015

1.5.4

1.5.4.0

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

08/10 2015

1.5.3

1.5.3.0

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

08/10 2015

1.5.2

1.5.2.0

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

08/10 2015

1.5.1

1.5.1.0

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

08/10 2015

1.5.0

1.5.0.0

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

06/10 2015

1.4.3

1.4.3.0

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

04/10 2015

1.4.2

1.4.2.0

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

04/10 2015

1.4.1

1.4.1.0

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

27/08 2015

1.4.0

1.4.0.0

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

25/08 2015

1.3.4

1.3.4.0

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

25/08 2015

1.3.3

1.3.3.0

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

20/08 2015

1.3.2

1.3.2.0

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

20/08 2015

1.3.1

1.3.1.0

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

20/08 2015

1.3.0

1.3.0.0

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

18/08 2015

1.2.0

1.2.0.0

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

15/07 2015

1.1.0

1.1.0.0

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha

15/07 2015

1.0.0

1.0.0.0

A very simple MVC support and routing.

  Sources   Download

The Development Requires

by Diogo Alexsander Cavilha