📜 ⬆️ ⬇️

SimplePage: a simple, declarative framework for rapid prototyping

I want to share with Habr a simple PHP framework that has grown out of minimalism ideas and aimed at the rapid development of simple websites.


I don’t want to seem like a toughening vparivayuschim you another framework, because all the links for a quick familiarization with the project leave over the cat



Sample article view page
<?php $sp = [ 'layout' => [ 'title' => '', ], 'input' => [ INPUT_GET => [ 'id' => [ FILTER_SANITIZE_NUMBER_INT, [ 'filter' => FILTER_VALIDATE_INT, 'options' => ['min_range' => 1], 'comment' => '   ,  ' ] ], ], ], 'pdo' => [ 'queries' => [ 'article' => [ 'SELECT * FROM article WHERE id = :id', 'params' => [ 'id' => &$_GET['id'], ], ], ], ], ]; include('../../sp.php'); $article = $article->fetch(); ?> <h1> <?= $article->title ?> </h1> <div> <?= $article->content ?> </div> <ul> <li> <a href="/articles/edit?id=<?= $article->id ?>">edit</a> </li> <li> <a href="/articles/delete.php?id=<?= $article->id ?>">delete</a> </li> </ul> 

Example of deletion of an article
 <?php <?php if($_SERVER['REQUEST_METHOD'] != 'GET'){ http_response_code(404); exit; } $sp = [ 'input' => [ INPUT_GET => [ 'id' => [ FILTER_SANITIZE_NUMBER_INT, [ 'filter' => FILTER_VALIDATE_INT, 'options' => ['min_range' => 1], 'comment' => '   ,  ' ] ], ], ], 'pdo' => [ 'queries' => [ [ 'DELETE FROM article WHERE id = :id', 'params' => [ 'id' => &$_GET['id'], ], ] ], ], ]; include('../sp.php'); header('Location: /articles', 302); 

For those interested, under the cut there will be a brief description of the project’s capabilities, its advantages and an example of its use.


Brief introduction


If you are familiar with Jekyll , then you have already met with most of the features of SimplePage, namely:



 <?php $sp = [ //       'layout' => [ 'title' => ' ', ], ]; include('../../sp.php'); ?> <form action="/articles/create.php" method="POST"> <div> <input type="text" name="title" placeholder=""/> </div> <div> <textarea name="content"></textarea> </div> <div> <input type="submit" value=""/> </div> </form> 


 â–ľ articles/ â–¸ _locale/ â–¸ create/ â–ľ edit/ index.php //    â–¸ view/ create.php //    delete.php edit.php index.php //     index.php //   


Advantages and disadvantages


Benefits:



Disadvantages:



Plugins and modules


To expand the functionality of the framework and develop application solutions using it, plug-ins and modules are used, respectively. The first, as a rule, represent the infrastructure and auxiliary components of the application, and the second applied solutions.


To connect the plugin, it is enough to specify it in the configuration:


 return [ 'plugins' => [ '_plugins/autoload.php', '_plugins/input.php', '_plugins/middleware.php', '_plugins/i18n.php', '_plugins/error.php', '_plugins/pdo.php', '_plugins/acl.php', '_plugins/layout.php', '_plugins/hook.php', ], ... ]; 

By default, the framework comes with the following plugins:



The developer can add these plug-ins to their own, including using external dependencies and composer:


 return [ 'plugins' => [ 'vendor/autoload.php', '_plugins/markdown.php', ... ], ... ]; 

Unlike plugins, modules are represented by a set of PHP scripts and even pages that solve application tasks of the project, such as the "Admin Panel" or "Forum".


Simple blog example


Consider an example of creating a blog using this framework.


To begin with, we will implement the general configuration of the site by creating the config.php file in the project root:


 <?php return [ 'plugins' => [ '_plugins/input.php', '_plugins/middleware.php', '_plugins/i18n.php', '_plugins/error.php', '_plugins/pdo.php', '_plugins/layout.php', ], 'layout' => [ 'title' => 'SimplePage', 'layout' => '_layout/default.html', ], 'pdo' => [ 'dsn' => [ 'mysql', 'dbname' => 'sp', 'charset' => 'UTF8', ], 'username' => 'root', 'password' => 'root', 'options' => [ PDO::ATTR_PERSISTENT => true ], ], ]; 

Next, we implement the layout by creating a template _layout / default.html, which will be applied to all pages of the site:


 <!DOCTYPE html> <html> <head> <title><?= i18n($title) ?></title> <meta charset="utf-8" /> <link href="/_css/style.css" rel="stylesheet"> </head> <body> <?= $content ?> </body> </html> 

It's time to create the main page of the site, for this we write the following to the index.php file:


 <?php include('sp.php') ?> <h1>Hello world</h1> <p>    <p> <ul> <li> <a href="/articles"></a> </li> </ul> 

Next, we will create a page with a list of articles (it is assumed that the tables for this module have already been created in the database), for this we write the following code into the file / index.php:


 <?php $start = isset($_GET['start'])? (int) $_GET['start'] : 0; $offset = isset($_GET['offset'])? (int) $_GET['offset'] : 2; $sp = [ 'layout' => [ 'title' => 'Articles', ], 'pdo' => [ 'queries' => [ 'articles' => [ 'SELECT * FROM article LIMIT :start, :offset', 'params' => [ 'start' => $start, 'offset' => $offset, ], ], 'articlesCount' => [ 'SELECT COUNT(*) FROM article', ], ], ], ]; include('../sp.php'); $articlesCount = $articlesCount->fetchColumn(); ?> <h1></h1> <p> <ul> <?php foreach($articles as $article): ?> <li> <a href="/articles/view?id=<?= $article->id ?>"> <?= $article->title ?> </a> </li> <?php endforeach; ?> </ul> <ul> <li>  <?= i18n_plural($articlesCount, '%d article') ?> </li> <li> <a href="/articles/create"><?= i18n('create') ?></a> </li> <li> <a href="/articles?start=<?= $start - $offset ?>"><?= i18n('prev') ?></a> </li> <li> <a href="/articles?start=<?= $start + $offset?>"><?= i18n('next') ?></a> </li> </ul> </p> 

As you can see, internationalization is used in the page template. Therefore, it is necessary to create the articles / _locale / ru_RU.php file for its proper operation:


 <?php return [ '' => [ 'plural_forms' => function($n){ if($n % 10 == 1 && $n % 100 != 11){ return 0; } elseif($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20)){ return 1; } else{ return 2; } }, ], '%d article' => [ '%d ', '%d ', '%d ', ], 'Articles' => '', 'create' => '', 'prev' => '', 'next' => '', ]; 

Lastly, we consider the action of creating a new article in the blog, for this we create the file articles / create.php:


 <?php if($_SERVER['REQUEST_METHOD'] != 'POST'){ http_response_code(404); exit; } $sp = [ 'pdo' => [ 'queries' => [ [ 'INSERT INTO article (title, content) VALUES (:title, :content)', 'params' => [ 'title' => $_POST['title'], 'content' => $_POST['content'], ], ], ], ], ]; include('../sp.php'); header('Location: /articles/view?id=' . pdo_build($sp['pdo'])->lastInsertId(), 302); 

')

Source: https://habr.com/ru/post/328002/


All Articles