soneritics/framework

Soneritics PHP 框架,适用于 PHP 5.5 及更高版本。

2.2.2 2019-09-24 09:55 UTC

This package is auto-updated.

Last update: 2024-09-21 02:32:59 UTC


README

Build Status Coverage Status License

简介

简要概述该框架的功能

Soneritics 框架是一个面向对象的框架,提供了创建网站和Web应用的一些非常有用的功能,使开发变得非常容易。

最低要求

  • PHP 5.5+
  • 您数据库的 PDO 驱动程序(目前仅支持 MySQL)

支持数据库

  • MySQL

特性

  • MVC 结构
  • 路由
  • 数据库查找器
  • REST
  • 等等

安装

设置非常简单直接。您可以选择以下选项之一

  1. 将框架上传到您的 include 路径。
  2. 将框架上传到比您的网站/应用程序文件夹高一级的私有目录。

然后通过包含引导文件启动框架

require_once('Framework/Bootstrap.php');
new Framework\Bootstrap;

数据库查询

数据库查询非常简单。下面有一些示例。

// Define the tables we have as Table extends
class Father extends Table {}
class Mother extends Table {}
class Child extends Table {}

// Use the Child table as a base for the queries
$child = new Child;

// Select everything from the children table
$child
    ->select()
    ->execute();

// Join a child with it's parents
$child
    ->select()
    ->leftJoin(new Father, 'Father.id = father_id')
    ->leftJoin(new Mother, 'Mother.id = mother_id')
    ->execute();

// A new child has been born!
$child
    ->insert()
    ->values([
        'firstname' => 'first name',
        'lastname' => 'last name',
        'father_id' => 1,
        'mother_id' => 1
    ])
    ->execute();

// Typo in the baby's name :-)
$child
    ->update()
    ->set('firstname', 'new first name')
    ->where([
        'firstname' => 'first name',
        'lastname' => 'last name'
    ])
    ->execute();

// Typo in the first and lastname of the baby :O
$child
    ->update()
    ->set(['firstname' => 'new first name', 'lastname' => 'new last name'])
    ->where([
        'firstname' => 'first name',
        'lastname' => 'last name'
    ])
    ->execute();

// Selecting with some sorting and limiting
$child
    ->select()
    ->leftJoin(new Father, 'Father.id = father_id')
    ->leftJoin(new Mother, 'Mother.id = mother_id')
    ->orderAsc('lastname')
    ->orderAsc('firstname')
    ->limit(25)
    ->execute();

用户输入

可以使用 FormHelper 处理用户输入。在视图中,FormHelper 通过 $form 属性访问。

以下示例展示了如何将用户添加到数据库中。

视图文件

    // Render the form's start tag <form>
    $this->form
        ->start()
        ->setMethod('post')
        ->setAction('/add-user')
        ->setParam('id', 'loginForm')
        ->setClass('form-horizontal')
        ->render();

    /* Render the inputs for the user's email and name.
     * When the user submits a form that is invalid, the
     * object automatically assigns an error class.
     * By using the setValueFromPost() function, the 
     * previously entered value gets set.
     */
    $this->form
        ->text('User.email')
        ->setParam('id', 'email')
        ->setParam('placeholder', 'Email address here')
        ->setClass('form-control')
        ->setValueFromPost()
        ->render();

    $this->form
        ->text('User.name')
        ->setParam('placeholder', 'Your name')
        ->setClass('form-control')
        ->setValueFromPost()
        ->render();

    $this->form->submit()->render();

    // Form's end tag
    $this->form->end()->render();

控制器

class User extends Controller
{
    public function addAction()
    {
        return new View('User/add');
    }

    public function addPost()
    {
        // Create the model, based on the Table object
        $userModel = new Models\User;

        // Validate data
        if (!$userModel->validates($this->fromPost('User'))) {
            return $this->addAction();
        }

        // Insert data and get the last insert id
        $userId = $userModel
            ->insert()
            ->set($this->fromPost('User'))
            ->execute();

        /* The user is now added to the database.
         * The following code can fetch the user data:
         * $userData = $userModel->select()->where(['id' => $userId])->execute()->get();
         *
         * This will return the following array:
         * [
         *     'User' => [
         *         'id' => 1,
         *         'email' => 'User email address',
         *         'name' => 'User name'
         *     ]
         * ]
         */

        // Done, redirect to the homepage
        new Redirect('/');
    }
}