{"id":17348259,"url":"https://github.com/diogocavilha/piano","last_synced_at":"2025-04-14T21:12:06.940Z","repository":{"id":42716043,"uuid":"39111502","full_name":"diogocavilha/Piano","owner":"diogocavilha","description":"A very simple MVC and routing application. It has been built for studying proposal.","archived":false,"fork":false,"pushed_at":"2022-03-27T14:03:45.000Z","size":494,"stargazers_count":6,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-14T21:11:58.421Z","etag":null,"topics":["php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/diogocavilha.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-15T02:20:36.000Z","updated_at":"2022-09-04T12:54:34.000Z","dependencies_parsed_at":"2022-08-27T21:20:50.848Z","dependency_job_id":null,"html_url":"https://github.com/diogocavilha/Piano","commit_stats":null,"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diogocavilha%2FPiano","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diogocavilha%2FPiano/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diogocavilha%2FPiano/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diogocavilha%2FPiano/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/diogocavilha","download_url":"https://codeload.github.com/diogocavilha/Piano/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248961236,"owners_count":21189993,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php"],"created_at":"2024-10-15T16:51:45.498Z","updated_at":"2025-04-14T21:12:06.905Z","avatar_url":"https://github.com/diogocavilha.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/diogocavilha/Piano.svg?branch=master)](https://travis-ci.org/diogocavilha/Piano)\n\n# Piano MVC\n\nThis is not a micro/mini/whatever framework nor claims to be.\n\nIt's only a very simple MVC structure.\n\n\u003e 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.\n\n# Creating the project structure\n\n```bash\ncomposer create-project piano/mvc:dev-application myProject\n```\n\n## Controller\n\n#### Redirecting\n\n```php\n$this-\u003eredirect('/module/controller/action');\n```\n\n#### Redirecting with variables\n\n```php\n$this-\u003eredirect(\n    '/module/controller/action',\n    [\n        'firstName' =\u003e 'Diogo',\n        'lastName' =\u003e 'Cavilha',\n    ]\n);\n```\n\n#### Getting params by name\n\n```php\n$id = $this-\u003egetParam('id');\n```\n\n#### Getting all params\n\n```php\n$params = $this-\u003egetParams();\n```\n\n## Methods from `Piano\\Mvc\\Controller`\n\n##### initialize()\n\nIf you need to create a constructor method for your controller, you can do this by creating a method called `initialize()` instead of `__construct()`.\nThe `__construct()` method is already being used by `Piano\\Mvc\\Controller`.\n\n#### Example:\n\n```php\n\u003c?php\n\nnamespace app\\modules\\application\\controllers;\n\nclass IndexController extends Piano\\Mvc\\Controller\n{\n    protected function initialize()\n    {\n        // Do some action before executing any other code of your controller.\n    }\n}\n```\n\n## View\n\n#### Rendering a view\n\n```php\n$this-\u003eview-\u003erender('view-name');\n```\n\n#### Rendering a view with variables\n\n```php\n$this-\u003eview-\u003erender('view-name', ['name' =\u003e 'Diogo']);\n\n// or\n\n$this-\u003eview-\u003eaddVar('name', 'Diogo');\n$this-\u003eview-\u003eaddVar('email', 'email@domain.com');\n\n$this-\u003eview-\u003erender('view-name');\n\n// or\n\n$this-\u003eview-\u003esetVars([\n    'name' =\u003e 'Diogo',\n    'email' =\u003e 'email@domain.com',\n]);\n\n$this-\u003eview-\u003erender('view-name');\n```\n\nSo, in the view code you can access the variable:\n\n```php\n\u003cp\u003eThe user name is: \u003c?php echo $name ?\u003e\u003c/p\u003e\n```\n\n#### Disabling/Enabling the layout\n\n```php\n$this-\u003eview-\u003edisableLayout(); // disabling\n$this-\u003eview-\u003edisableLayout(true); // disabling\n$this-\u003eview-\u003edisableLayout(false); // enabling\n```\n\n#### Adding a partial\n\n```php\n$this-\u003epartial('/path/to/file');\n```\n\n#### Adding a partial with variables\n\n```php\n$this-\u003epartial('/path/to/file', ['title' =\u003e 'Piano MVC rocks!']);\n```\n\n#### Loading CSS files\n\nYou can load CSS files on demand. It means you can define what CSS files are gonna be loaded when some specific view is rendered.\nBetter than that, you can do this for each of your views.\n\n\nYou can use the `addCss()` method.\n\n```php\n$this-\u003eview-\u003eaddCss('/path/to/file1.css');\n$this-\u003eview-\u003eaddCss('/path/to/file2.css');\n$this-\u003eview-\u003eaddCss('/path/to/file3.css');\n\n$this-\u003eview-\u003erender('view-name');\n\n// or\n\n$this-\u003eview\n    -\u003eaddCss('/path/to/file1.css')\n    -\u003eaddCss('/path/to/file2.css')\n    -\u003eaddCss('/path/to/file3.css')\n    -\u003erender('view-name');\n```\n\nOr you may want to use the `setCss()` method.\n\n```php\n$this-\u003eview-\u003esetCss([\n    '/path/to/file1.css',\n    '/path/to/file2.css',\n    '/path/to/file3.css',\n]);\n\n$this-\u003eview-\u003erender('view-name');\n\n// or\n\n$this-\u003eview\n    -\u003esetCss([\n        '/path/to/file1.css',\n        '/path/to/file2.css',\n        '/path/to/file3.css',\n    ])\n    -\u003erender('view-name');\n\n```\n\n\u003e PS: You must call `addCss()` or `setCss()` method before calling the `render()` method. Otherwise it won't work.\n\n#### Loading javascript files\n\nYou can load javascript files on demand. It means you can define what javascript files are gonna be loaded when some specific view is rendered.\nBetter than that, you can do this for each of your views.\n\nYou can use the `addJs()` method.\n\n```php\n$this-\u003eview-\u003eaddJs('/path/to/file1.js');\n$this-\u003eview-\u003eaddJs('/path/to/file2.js');\n$this-\u003eview-\u003eaddJs('/path/to/file3.js');\n\n$this-\u003eview-\u003erender('view-name');\n\n// or\n\n$this-\u003eview\n    -\u003eaddJs('/path/to/file1.js')\n    -\u003eaddJs('/path/to/file2.js')\n    -\u003eaddJs('/path/to/file3.js')\n    -\u003erender('view-name');\n```\n\nOr you may want to use the `setJs()` method.\n\n```php\n$this-\u003eview-\u003esetJs([\n    '/path/to/file1.js',\n    '/path/to/file2.js',\n    '/path/to/file3.js',\n]);\n\n$this-\u003eview-\u003erender('view-name');\n\n// or\n\n$this-\u003eview\n    -\u003esetJs([\n        '/path/to/file1.js',\n        '/path/to/file2.js',\n        '/path/to/file3.js',\n    ])\n    -\u003erender('view-name');\n\n```\n\n\u003e PS: You must call `addJs()` or `setJs()` method before calling the `render()` method. Otherwise it won't work.\n\nIn order to load these CSS or JS files in your view/layout you can call the `loadCss()` or `loadJs()` method.\n\n```php\n// Loading the js files\n$this-\u003eloadJs();\n\n// Loading the css files\n$this-\u003eloadCss();\n```\n\n#### href links\n\n```\n\u003ca href=\"\u003c?php echo $this-\u003eurl('route_name'); ?\u003e\"\u003eText\u003c/a\u003e\n```\n\n#### Form action must be written by using a pre-defined route\n\n```\n\u003cform action=\"\u003c?php echo $this-\u003eurl('contact'); ?\u003e\" method=\"post\"\u003e\n    Name: \u003cinput type=\"text\" name=\"name\"\u003e\n    Email: \u003cinput type=\"text\" name=\"email\"\u003e\n    \u003cinput type=\"submit\" name=\"Send\"\u003e\n\u003c/form\u003e\n```\n\n## Model\n\n#### The DataAccessAbstract class.\n\nThe `Piano\\Mvc\\DataAccessAbstract` abstract class provides us a few methods for handling data by accessing the database.\n\n#### Attributes from `Piano\\Mvc\\DataAccessAbstract`\n\n```php\nprotected $table;\n```\n\u003e Table name for working.\n\n```php\nprotected $model;\n```\n\u003e If not null, it's used for fetching its model. Otherwise, it will use an associative array.\n\n```php\nprotected $pdo;\n```\n\u003e A PHP PDO instance.\n\n---\n\n#### Methods from `Piano\\Mvc\\DataAccessAbstract`\n\n```php\ninsert(array $data, array $dataBind)\n```\nCreate a record into database.\n\n#### Parameters\n\n**data**\n\u003e Array data to insert into the database table.\n\n**dataBind**\n\u003e The bound values.\n\n#### Return Values\n\n\u003e Returns the last insert id on success or FALSE on failure.\n\n---\n\n```php\nupdate(array $data, $where, array $dataBind)\n```\nChange a database record.\n\n#### Parameters\n\n**data**\n\u003e Array data to insert into the database table.\n\n**where**\n\u003e Where clause.\n\n**dataBind**\n\u003e The bound values.\n\n#### Return Values\n\n\u003e Returns TRUE on success or FALSE on failure.\n\n---\n\n```php\ndelete($where, array $dataBind = array())\n```\nDelete a record from database.\n\n#### Parameters\n\n**where**\n\u003e Where clause.\n\n**dataBind**\n\u003e The bound values.\n\n#### Return Values\n\n\u003e Returns TRUE on success or FALSE on failure.\n\n---\n\n```php\ngetAll([$configData = null, $order = null, $count = null, $offset = null])\n```\nPerform a query in order to return all database records.\n\n#### Parameters\n\n**configData**\n\u003e The query configuration.\n\u003e\n\u003e Example:\n\u003e\n```php\n$configData = array(\n    'fetchClass' =\u003e false,\n    'columns' =\u003e '*',\n    'condition' =\u003e 'id = :id',\n    'values' =\u003e array(\n        array(':id', 1, PDO::PARAM_INT)\n    )\n);\n```\n\n**order**\n\u003e Like SQL ORDER.\n\u003e\n\u003e Example:\n```php\n'id ASC, name ASC'\n```\n\n**count**\n\u003e Integer value used to make the query return a specific set of rows.\n\n**offset**\n\u003e Integer value used to make the query return a specific set of rows.\n\u003e\n\u003e PS: When both count and offset are used, the query to be executed has LIMIT offset, count\n\n\n#### Return Values\n\n\u003e - When fetchClass parameter is true or omitted, it returns an array of model objects.\n\u003e - When fetchClass parameter is false, it returns an associative array.\n\n---\n\n```php\ngetFirst($configData = null, $order = null)\n```\nPerform a query in order to return all database records.\n\n#### Parameters\n\n**configData**\n\u003e The query configuration.\n\u003e\n\u003e Example:\n\u003e\n```php\n$configData = array(\n    'fetchClass' =\u003e false,\n    'columns' =\u003e '*',\n    'condition' =\u003e 'id = :id',\n    'values' =\u003e array(\n        array(':id', 1, PDO::PARAM_INT)\n    )\n);\n```\n\n**order**\n\u003e Like SQL ORDER.\n\u003e\n\u003e Example:\n```php\n'id ASC, name ASC'\n```\n\n#### Return Values\n\n\u003e - When fetchClass parameter is true or omitted, it returns a model object.\n\u003e - When fetchClass parameter is false, it returns an associative array.\n\n---\n\n### Examples:\n\n##### insert()\n```php\n$pdo = new PDO(\"mysql:host=host;dbname=db;\", 'user', 'pass');\n\n$userDAO = new \\app\\dataAccess\\UserDataAccess($pdo);\n$id = $userDAO-\u003einsert(\n                    array(\n                       'name' =\u003e ':name',\n                       'email' =\u003e ':email',\n                    ), array(\n                       array(':name', 'John Doe', PDO::PARAM_STR),\n                       array(':email', 'john@domain.com', PDO::PARAM_STR),\n                    )\n                );\n```\n\n##### update()\n```php\n$pdo = new PDO(\"mysql:host=host;dbname=db;\", 'user', 'pass');\n\n$userDAO = new \\app\\dataAccess\\UserDataAccess($pdo);\n$status = $userDAO-\u003eupdate(\n                        array(\n                            'name' =\u003e ':new_name',\n                        ),\n                        'name = :where_name',\n                        array(\n                           array(':new_name', 'New Name', PDO::PARAM_STR),\n                           array(':where_name', 'Old Name', PDO::PARAM_STR),\n                       )\n                    );\n```\n\n##### delete()\n```php\n$pdo = new PDO(\"mysql:host=host;dbname=db;\", 'user', 'pass');\n\n$userDAO = new \\app\\dataAccess\\UserDataAccess($pdo);\n$status = $userDAO-\u003edelete(\n                        'id = :id',\n                        array(\n                           array(':id', 2, PDO::PARAM_INT),\n                       )\n                    );\n\n// or\n\n$userDAO = new \\app\\dataAccess\\UserDataAccess($pdo);\n$status = $userDAO-\u003edelete('id = 2');\n\n```\n\n##### getAll()\n```php\n$pdo = new PDO(\"mysql:host=host;dbname=db;\", 'user', 'pass');\n\n$userDAO = new \\app\\dataAccess\\UserDataAccess($pdo);\n$users = $userDAO-\u003egetAll(\n                        array(\n                           'columns' =\u003e '*',\n                           'condition' =\u003e 'id = :id',\n                           'values' =\u003e array(\n                               array(':id', 1, PDO::PARAM_INT),\n                           )\n                        ),\n                        'id DESC',\n                        10, // show 10 records\n                        30 // start showing from the 30th record\n                    );\n\n// or\n\n$userDAO = new \\app\\dataAccess\\UserDataAccess($pdo);\n$users = $userDAO-\u003egetAll();\n```\n\n##### getFirst()\n```php\n$pdo = new PDO(\"mysql:host=host;dbname=db;\", 'user', 'pass');\n\n$userDAO = new \\app\\dataAccess\\UserDataAccess($pdo);\n$user = $userDAO-\u003egetFirst(\n                        array(\n                           'columns' =\u003e '*',\n                           'condition' =\u003e 'id = :id',\n                           'values' =\u003e array(\n                               array(':id', 1, PDO::PARAM_INT)\n                           )\n                        )\n                    );\n\n// or\n\n$userDAO = new \\app\\dataAccess\\UserDataAccess($pdo);\n$user = $userDAO-\u003egetFirst();\n```\n\n---\n\n## Application\n\n#### Getting module name\n\n```php\n$this-\u003egetApplication()-\u003egetModuleName();\n```\n\n#### Getting controller name\n\n```php\n$this-\u003egetApplication()-\u003egetControllerName();\n```\n\n#### Getting action name\n\n```php\n$this-\u003egetApplication()-\u003egetActionName();\n```\n\n---\n\n## Class `Piano\\Config\\Ini`\n\nThe `Piano\\Config\\Ini` is a ini file parser. You must pass the ini path as an argument to its constructor.\n\n##### Sample\n\n```php\n$config = new Piano\\Config\\Ini('/path/to/config.ini');\n$configIni = $config-\u003eget();\n\n// getting a simple value\n\n$configIni = $config-\u003eget('name');\n// If \"name\" doesn't exist in config file, it will return an empty array.\n\n// getting a section\n\n$configIni = $config-\u003eget('section_name');\n// If section_name doesn't exist in config file, it will return an empty array.\n```\n\n## Class `Piano\\Config\\Pdo`\n\nThe `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.\n\n##### Sample\n\n```php\n// The config array must be like this.\n$config = [\n    'dbAdapter' =\u003e 'mysql',\n    'dbHost'    =\u003e 'localhost',\n    'dbName'    =\u003e '',\n    'dbUser'    =\u003e '',\n    'dbPass'    =\u003e '',\n];\n\n$pdo = new Piano\\Config\\Pdo($config);\n$pdo = $pdo-\u003eget();\n```\n\n## Class `Piano\\Router`\n\nThe `Piano\\Router` is a route parser. It helps you to define the application's routes.\n\n##### Sample\n\n```php\n$router = new Piano\\Router();\n```\n\nYou're able to define all routes as an array or by using the `Piano\\Router` object notation.\n\nDefining routes as array.\n\n```php\n$routes = [\n    'default' =\u003e [\n        'route' =\u003e '/',\n        'module' =\u003e 'application',\n        'controller' =\u003e 'index',\n        'action' =\u003e 'index',\n    ],\n    'contact' =\u003e [\n        'route' =\u003e '/contact',\n        'module' =\u003e 'application',\n        'controller' =\u003e 'index',\n        'action' =\u003e 'contact',\n    ],\n    'userEdit' =\u003e [\n        'route' =\u003e '/user/:id',\n        'module' =\u003e 'application',\n        'controller' =\u003e 'user',\n        'action' =\u003e 'edit',\n        [\n            ':id' =\u003e '\\d+'\n        ]\n    ],\n];\n\n$router-\u003esetRoutes($routes);\n```\n\nDefining routes as object.\n\n```php\n$router-\u003eaddRoute('default', '/', [\n    'module' =\u003e 'application',\n    'controller' =\u003e 'index',\n    'action' =\u003e 'index',\n]);\n\n$router-\u003eaddRoute('contact', '/contact', [\n    'module' =\u003e 'application',\n    'controller' =\u003e 'index',\n    'action' =\u003e 'contact',\n]);\n\n$router-\u003eaddRoute('userEdit', '/user/:id', [\n    'module' =\u003e 'application',\n    'controller' =\u003e 'index',\n    'action' =\u003e 'contact',\n    [\n        ':id' =\u003e '\\d+'\n    ]\n]);\n```\n\nYou can also enable/disable SEF. *(It's useful to disable it when developing)*\nIt can be done by calling the method `enableSearchEngineFriendly`\n\n```php\n$router-\u003eenableSearchEngineFriendly(); // Enable\n$router-\u003eenableSearchEngineFriendly(true); // Enable\n$router-\u003eenableSearchEngineFriendly(false); // Disable\n```\n\nIf you want to get all routes or a particular one, you could do like this:\n\n```php\n// It'll return all your routes definitions.\n$router-\u003egetRoutes();\n\n// It'll return informations from the given route, in this case `myRouteName`\n$router-\u003egetRoutes('myRouteName');\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiogocavilha%2Fpiano","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdiogocavilha%2Fpiano","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiogocavilha%2Fpiano/lists"}