saturn / hyperion
轻量级MVC框架
Requires
- php: >=5.3.0
README
Hyperion是一个轻量级MVC框架,它让编程回归了乐趣!或者,你知道的,随便什么。Hyperion使得创建MVC应用程序变得超级简单。
附加功能:没有近亲繁殖(这不能用于偷我们名字的希腊神祇)。
要求
- PHP >= 5.3.
- Apache web服务器,支持mod_rewrite模块。
- MySQL数据库(可选)。
安装
在您的根目录中添加一个 .htaccess 文件并创建重写规则。请求应重定向到 public
目录中的 index.php 文件,例如
Options -Indexes
RewriteEngine on
RewriteBase /public
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_METHOD} ^(get|post)$ [NC]
RewriteRule ^ index.php [QSA,L]
使用Composer(推荐)
Hyperion被设计为Composer库包。因此,安装Hyperion的最简单但可能不是最干净的方法是使用Composer。这将把框架添加到 vendor
目录中,这会隐藏一些不那么丑陋但无聊的内核文件,并帮助您专注于创建您的 SUPER HAPPY AWESOME FUN(简称SHAF)应用程序,而不用担心文件结构中的近亲繁殖神祇。只需在您项目的根目录中运行以下命令即可。
$ composer require saturn/hyperion
源代码
当然,您也可以下载源文件或克隆git仓库并将其手动添加到您选择的文件夹中。建议您将其放入根目录级别的单独文件夹中,例如hyperion
。
$ git clone git://github.com/pkrll/Hyperion.git
设置
- Hyperion依赖于以下概述的文件夹结构。因此,请重建此结构
- application/
- controllers/
+ Custom controllers goes here (ie ExampleController.php).
- includes/
+ The UI includes resides here, with its own folder structure (ie Example/main.inc)
- models/
+ Custom models goes here (ie ExampleModel.php).
- templates/
+ The UI templates resides here, with its own folder structure (ie Example/main.tpl).
- views/
+ Custom views goes here (ie ExampleView.php).
- public/
- css/ (optional)
+ Stylesheet files.
- images/ (optional)
+ System images and uploads.
- javscript/ (optional)
+ JavaScript files.
- index.php
- .htaccess
- 如果您想在项目中使用数据库,请将Hyperion文件夹中的
config.php
文件中的数据库特定常量设置为适当的配置值。 - 接下来,您需要包含文件。根据安装方法,此步骤可能会有所不同。
使用Composer
- 如果使用Composer安装,则
vendor
文件夹应已添加到根目录中。框架就在那个文件夹里。使用Composer的自动加载方法导入文件,只需将以下内容添加到public
文件夹中的index.php顶部即可
use hyperion\core\Bootstrap; include dirname(__DIR__)."/vendor/autoload.php";
手动安装
- 如果手动安装,则需要包含Hyperion文件(除了config.php)。以下是一个示例
// index.php use hyperion\core\Bootstrap; include dirname(__DIR__)."/hyperion/core/Bootstrap.php"; include dirname(__DIR__)."/hyperion/core/Controller.php"; include dirname(__DIR__)."/hyperion/core/Model.php"; include dirname(__DIR__)."/hyperion/core/View.php"; include dirname(__DIR__)."/hyperion/library/Database.php";
引导
- 在您可以使用Hyperion创建您的 WORLD SHATTERING SHAF(简称WSSHAF)应用程序之前,您需要调用Bootstrap类。在index.php的末尾添加以下代码。
$App = new Bootstrap();
用法
所有控制器都必须放在application/controllers
文件夹中。控制器还必须遵循特定的命名约定,其中文件和类名必须以大写字母开头并以Controller结尾。例如,如果我们想创建一个名为SHAF的控制器,那么文件将被称为SHAFController.php
,而类名为SHAFController
。控制器类还必须扩展基本控制器Controller
。
- 注意:所有控制器都必须具有默认的主方法,如果没有指定任何操作(即hyperion.dev/example),则将调用该方法。foo()是当URL请求路径被设置并且传递了一个参数时的情况(即hyperion.dev/example/foo/bar)。
ExampleController.php
use hyperion\core\Controller; class ExampleController extends Controller { public function main () { // Let's get a message from our overlords // in the Model class. $message = $this->model()->getMessageFromOverlords(); // Assign the variable to the view // class to send to the template: $this->view()->assign("message", $message); // Render the template file (inside // the application/templates folder): $this->view()->render("Example/main.tpl"); } // hyperion.dev/example/foo/bar public function foo() { // set $argument to the parameter ("bar") $argument = $this->arguments[0]; // Use $this->arguments[n] if there are more parameters ... } }
模型遵循与控制器和视图类相同的命名约定。
ExampleModel.php
use hyperion\core\Model; class ExampleModel extends Model { public function getMessageFromOverlords() { // This is where the app logic goes // ... $response = "This is SHAF app!"; return $response; } }
example/main.tpl
<html> <head><title>A SHAF app example</title></head> <body> <?=$message?> <!-- prints out: This is a SHAF app! --> </body> </html>
数据库支持
想要使用数据库?没问题。如果不是SHAF应用的话,那就不会有了。Hyperion附带了一个简单的数据库类(使用PDO),支持MySQL。为了让它对你来说超级简单并且极其SHAF(让我们称之为EWSSHAF),所有你的模型类都继承的基础模型,提供了一系列用于访问简单和基本PDO命令的方法。以下是在创建你的EWSSHAF应用时可以使用的方法列表。
// Model.php /** * Prepare an SQL statement for execution. * @param string The SQL statement to prepare. */ final protected function prepare($query); /** * Bind a value to a named or question mark placeholder * in the prepared SQL statement. * @param mixed The parameter identifier. For named * placeholder, this value must be a * string (:name). For a question mark * placeholder, the value must be the * 1-indexed position of the parameter. * @param mixed The value to bind to the parameter. * @param int Data type for the parameter, using * the predefined PDO constants: * https://php.ac.cn/manual/en/pdo.constants.php * @return bool */ final protected function bindValue($param, $value, $dataType = PDO::PARAM_STR); /** * Bind a referenced variable to a named or question mark * placeholder in the prepared SQL statement. * @param mixed The parameter identifier. For named * placeholder, this value must be a * string (:name). For a question mark * placeholder, the value must be the * 1-indexed position of the parameter. * @param mixed Variable to bind to the parameter. * @param int Data type for the parameter, using * the predefined PDO constants: * https://php.ac.cn/manual/en/pdo.constants.php * @return bool */ final protected function bindParam($param, &$value, $dataType = PDO::PARAM_STR); /** * Run the supplied query. Only for fetching rows from * the database. * @param string Optional. The SQL query to execute. * @param array Optional. Additional parameters to * supply to the query. * @param bool If true, fetches all matching rows. * Defaults to TRUE. * @return mixed A list of matching rows or on error * an array with the error message. **/ final protected function read($query = NULL, $params = NULL, $fetchAll = TRUE); /** * Run the supplied query. Only for adding rows to the * the database. * @param string Optional. The SQL query to execute. * @param array Optional. Additional parameters to * supply to the query. * @return mixed On success: the last inserted id or. * On error: An array with the error. **/ final protected function write($query = NULL, $params = NULL); /** * Return the number of rows affected by the last SQL * statement performed. * @return int */ final protected function rowCount();
困惑了吗?我也是。但是,嘿,让我们看看如何使用它来制作可爱EWSSHAF(是的,你已经猜到了,CEWSSHAF!好吧,我得停下来……)应用。
// ExampleModel.php use hyperion\core\Model; class ExampleModel extends Model { public function getMessageFromOverlords() { // Prepare the query $this->prepare("SELECT Message FROM OverlordMessageBoard"); // Call the read() method from the parent class Model. $response = $this->read(); // The above can also be written: // $response = $this->read("SELECT Message FROM OverlordMessageBoard"); return $response; } }
没有使用带--参数的语句的美味CEWSSHAF(哦,你知道的,DCEWSSHAF。说真的,我停不下来,我可能有严重的心理问题,帮帮我……)应用是什么?
// ExampleModel.php use hyperion\core\Model; class ExampleModel extends Model { public function addMessage($message) { // The $message array would look like this: // array( // "message" => "DCEWSSHAF apping!!", // "date" => time() // ); $sqlQuery = "INSERT INTO Messages (message, date) VALUES(:message, :date)"; $this->prepare($sqlQuery); $this->bindValue(":message", $message['message']); $this->bindValue(":date", $message['date']); $response = $this->write(); return $response; } public function deleteMessage($id) { $sqlQuery = "DELETE FROM Messages WHERE id = :id"; $sqlParam = array("id" => $id); // We did not use prepare() – do not worry, // the write/read methods will prepare the // statement for your if supplied directly. $response = $this->write($sqlQuery, $sqlParam); return $response; } // Lets do a more complicated example public function addMessages(array $messages) { // The $messages array looks like this: // $messages[] = array( // "message" => "Message 1", // "date" => time() // ); // $messages[] = array( // "message" => "Message 2", // "date" => time() // ); // $messages[] = array( // "message" => "Message 3", // "date" => time() // ); // Create the question marked placeholders, based on // the number of values the row will take, (?,?...). $markers = array_fill(0, count($messages[0]), '?'); $markers = '(' . implode(", ", $markers) . ')'; // The number of placeholders must match the number // of values that are to be inserted in the VALUES- // clause. Create the array with array_fill() and // join the array with the query-string. $sqlClause = array_fill(0, count($messages), $markers); $sqlQuery = "INSERT INTO Messages (message, date) VALUES " . implode(", ", $sqlClause); $this->prepare($sqlQuery); // Bind the values using bindValue(). // Using question marked placeholders, // the value must be 1-indexed, that // is starting at position 1. $index = 1; foreach ($messages AS $key => $message) { $this->bindValue($index++, $message['message']); $this->bindValue($index++, $message['date']); } // A more pretty and dynamic way to write // the above statement could be by going // by the columns of the array, like so: // $columns = array_keys($messages[0]); //foreach ($messages AS $key => $message) { // foreach ($columns AS $column) // $this->bindValue($position++, $messages[$column]); // Write to database $response = $this->write(); return $response; } }
作者
- Hyperion由Ardalan Samimi创建。