PHP code example of saturn / hyperion

1. Go to this page and download the library: Download saturn/hyperion library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

saturn / hyperion example snippets


use hyperion\core\Bootstrap;

// index.php
use hyperion\core\Bootstrap;
Controller.php";
e dirname(__DIR__)."/hyperion/library/Database.php";

$App = new Bootstrap();

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
       ...
    }
}

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;
    }
}

// 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:
 *                  http://php.net/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:
 *                  http://php.net/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();

// 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;
    }
}

// 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;
    }
}