This is not a micro/mini/whatever framework nor claims to be.
It's only a very simple MVC structure.
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.
composer create-project piano/mvc:dev-application myProject
$this->redirect('/module/controller/action');
$this->redirect(
'/module/controller/action',
[
'firstName' => 'Diogo',
'lastName' => 'Cavilha',
]
);
$id = $this->getParam('id');
$params = $this->getParams();
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
.
<?php
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@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:
<p>The user name is: <?php echo $name ?></p>
$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!']);
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.
You can use the addCss()
method.
$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.
$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()
orsetCss()
method before calling therender()
method. Otherwise it won't work.
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.
You can use the addJs()
method.
$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.
$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()
orsetJs()
method before calling therender()
method. Otherwise it won't work.
In order to load these CSS or JS files in your view/layout you can call the loadCss()
or loadJs()
method.
// Loading the js files
$this->loadJs();
// Loading the css files
$this->loadCss();
<a href="<?php echo $this->url('route_name'); ?>">Text</a>
<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>
The Piano\Mvc\DataAccessAbstract
abstract class provides us a few methods for handling data by accessing the database.
protected $table;
Table name for working.
protected $model;
If not null, it's used for fetching its model. Otherwise, it will use an associative array.
protected $pdo;
A PHP PDO instance.
insert(array $data, array $dataBind)
Create a record into database.
data
Array data to insert into the database table.
dataBind
The bound values.
Returns the last insert id on success or FALSE on failure.
update(array $data, $where, array $dataBind)
Change a database record.
data
Array data to insert into the database table.
where
Where clause.
dataBind
The bound values.
Returns TRUE on success or FALSE on failure.
delete($where, array $dataBind = array())
Delete a record from database.
where
Where clause.
dataBind
The bound values.
Returns TRUE on success or FALSE on failure.
getAll([$configData = null, $order = null, $count = null, $offset = null])
Perform a query in order to return all database records.
configData
The query configuration.
Example:
$configData = array(
'fetchClass' => false,
'columns' => '*',
'condition' => 'id = :id',
'values' => array(
array(':id', 1, PDO::PARAM_INT)
)
);
order
Like SQL ORDER.
Example:
'id ASC, name ASC'
count
Integer value used to make the query return a specific set of rows.
offset
Integer value used to make the query return a specific set of rows.
PS: When both count and offset are used, the query to be executed has LIMIT offset, count
- 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.
configData
The query configuration.
Example:
$configData = array(
'fetchClass' => false,
'columns' => '*',
'condition' => 'id = :id',
'values' => array(
array(':id', 1, PDO::PARAM_INT)
)
);
order
Like SQL ORDER.
Example:
'id ASC, name ASC'
- When fetchClass parameter is true or omitted, it returns a model object.
- When fetchClass parameter is false, it returns an associative array.
$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),
)
);
$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();
The Piano\Config\Ini
is a ini file parser. You must pass the ini path as an argument to its constructor.
$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 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.
// The config array must be like this.
$config = [
'dbAdapter' => 'mysql',
'dbHost' => 'localhost',
'dbName' => '',
'dbUser' => '',
'dbPass' => '',
];
$pdo = new Piano\Config\Pdo($config);
$pdo = $pdo->get();
The Piano\Router
is a route parser. It helps you to define the application's routes.
$router = new Piano\Router();
You're able to define all routes as an array or by using the Piano\Router
object notation.
Defining routes as array.
$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.
$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
$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:
// It'll return all your routes definitions.
$router->getRoutes();
// It'll return informations from the given route, in this case `myRouteName`
$router->getRoutes('myRouteName');