mochrira/selvi-firebase

将 Firebase 与 selvi 框架集成的包

1.0.3 2020-12-05 06:04 UTC

README

将 selvi 框架与 Firebase 集成的最快方式

设置

在您的命令行界面执行以下行

$ composer require mochrira/selvi-framework
$ composer require mochrira/selvi-firebase

配置(单个数据库)

通过在您的 index.php 中的 Selvi\Framework::run() 之前添加以下行来设置此包

\Selvi\Firebase\Manager::setup(['serviceAccountFile' => 'path to your service account json']);
\Selvi\Firebase\Pengguna::setup(['schema' => 'your main schema']);

设置数据库

通过在您的项目目录中运行以下命令来设置您的数据库

$ php index.php migrate main up

然后,检查您的数据库,您将看到 Firebase 项目的默认数据库结构

接受授权头

要接受授权头,请将以下行添加到您的 .htaccess 文件的末尾

RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

然后,将以下内容添加到您的 index.php 文件的顶部

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type, Authorization, authorization");

创建自定义控制器

为了验证对您的控制器发出的每个请求,您必须为您的应用程序创建自定义控制器。在您的项目的 app 文件夹中创建 Controller.php,并用以下脚本填充

<?php 

namespace App;
use Selvi\Controller as SelviController;
use Selvi\Firebase\Pengguna;

class Controller extends SelviController{

    function validateToken() {
        Pengguna::validateToken();
    }

    function validatePengguna() {
        Pengguna::validatePengguna();
    }

}

最后一步,将上述类扩展到您的所有应用程序控制器中,例如 KontakController,然后像下面一样调用 validateTokenvalidatePengguna

<?php 

namespace App\Controllers;
use App\Controller; /** import your custom controller */
use Selvi\Exception;
use App\Models\Kontak;

class KontakController extends Controller { /** extends that*/

    function __construct() {
        $this->validateToken(); /** To validate token **/
        $this->validatePengguna(); /** To validate pengguna **/
        $this->load(Kontak::class, 'Kontak');
    }

    function rowException($idKontak) {
        $data = $this->Kontak->row($idKontak);
        if(!$data) {
            Throw new Exception('Kontak not found', 'kontak/not-found', 404);
        }
        return $data;
    }

    function result() {
        $order = [];
        $sort = $this->input->get('sort');
        if($sort !== null) {
            $order = \buildOrder($sort);
        }

        $orWhere = [];
        $search = $this->input->get('search');
        if($search !== null) {
            $orWhere = \buildSearch(['nmKontak'], $search);
        }

        $where = [];
        return jsonResponse($this->Kontak->result($where, $orWhere, $order));
    }

    function row() {
        $idKontak = $this->uri->segment(2);
        $data = $this->rowException($idKontak);
        return jsonResponse($data);
    }

    function insert() {
        $data = json_decode($this->input->raw(), true);
        $idKontak = $this->Kontak->insert($data);
        if($idKontak === false) {
            Throw new Exception('Failed to insert', 'kontak/insert-failed');
        }
        return jsonResponse(['idKontak' => $idKontak], 201);
    }

    function update() {
        $idKontak = $this->uri->segment(2);
        $this->rowException($idKontak);
        $data = json_decode($this->input->raw(), true);
        if(!$this->Kontak->update($idKontak, $data)) {
            Throw new Exception('Failed to insert', 'kontak/insert-failed');
        }
        return response('', 204);
    }

    function delete() {
        $idKontak = $this->uri->segment(2);
        $this->rowException($idKontak);
        if(!$this->Kontak->delete($idKontak)) {
            Throw new Exception('Failed to insert', 'kontak/insert-failed');
        }
        return response('', 204);
    }

}