simbio/simbio3

SIMBIO 3 PHP 框架

v1.0.0 2016-08-01 16:02 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:25:41 UTC


README

Simbio 3 是一个简单的 PHP 框架,主要用于 SLiMS (Senayan Library Management System) 项目

安装

要在您的 Web 项目中安装 Simbio,只需在您的 Web 项目目录中运行 Composer 命令行即可

composer require simbio/simbio3

或者如果您不使用 Composer 手动安装,只需将所有 Simbio 源代码放入某个目录中(例如:simbio3),然后在 index.php 的顶部包含它

<?php
require_once 'simbio3/Simbio.php'
require_once 'simbio3/SimbioController.php'

自动加载配置

要使 composer 自动加载所有 Simbio 类,您需要编辑您的 Web 项目目录中的 composer.json 文件,使其看起来如下

{
    "require": {
        "simbio/simbio3": "*"
    },
    "autoload": {
        "psr-4": {
            "Simbio\\": "./vendor/simbio/simbio3/src/"
        }
    }
}

在更改 composer.json 文件后,您需要通过在命令行中调用以下命令来重新加载 composer 自动加载配置索引以使其生效

composer dump-autoload -o

使用 Simbio 路由类

在您的 Web 项目根目录中创建 index.php,包含以下代码

<?php
require __DIR__.'/vendor/autoload.php';

$simbio = new Simbio\Simbio;
try {
    $simbio->route();
} catch (Exception $error) {
    exit('Error : '.$error->getMessage());
}

创建以下最小目录结构

/my-web-project-dir/apps/
/my-web-project-dir/apps/modules/
/my-web-project-dir/apps/config/
/my-web-project-dir/apps/themes/

创建应用程序模块

要为您的 Web 项目创建模块,只需遵循以下简单步骤

  • /my-web-project-dir/apps/modules 下方创建目录。例如,对于“Bibliography”模块,目录将是 /my-web-project-dir/apps/modules/Bibliography,第一个字母必须是大写
  • Bibliography 模块目录中,创建一个名为 Bibliography.php 的文件,其中包含 Bibliography 类的定义
  • Bibliography 目录中创建 viewsmodels 目录
  • Bibliography 类中,您必须编写以下最小代码

Bibliography 控制器源代码

<?php
class Bibliography extends \Simbio\SimbioController {
	 /**
	  * The parent class \Simbio\SimbioController constructor MUST be
	  * also invoked in controller constructor
	  *
	  **/
    public function __construct($apps_config) {
        parent::__construct($apps_config);
    }

    /**
     * Default route, will be called when there is request to "https:///my-web-project-dir/index.php/Bibliography
     *
     **/
    public function index() {
        echo 'Hello! this is Simbio 3 framework';
    }

    /**
     * Route for request: "https:///my-web-project-dir/index.php/Bibliography/save/1"
     *
     **/
    public function save($id) {
        // SimbioController already provide $this->db PDO's object for database operation
        $stmt = $this->db->prepare("UPDATE biblio SET title=? WHERE biblio_id=?");
        $stmt->bindValue(1, 'New updated title', PDO::PARAM_STR);
        $stmt->bindValue(2, $id, PDO::PARAM_INT);
        $stmt->execute();
        // load view "update" from inside "Bibliography" module directory
        $this->loadView('update');
    }
}