vitodtagliente/pure-core

dev-master 2020-03-02 22:13 UTC

This package is auto-updated.

Last update: 2024-09-29 05:27:53 UTC


README

主框架核心功能。

配置管理

Config类允许管理应用程序的配置,加载.ini配置文件。

设置配置基本路径

Pure\Config::path("mypath...");

这意味着默认情况下配置文件将在这个路径下找到。

考虑以下ini文件,它位于app/Config/app.ini

; Common application settings
one = 1
name = "config_test"

要检索这些设置,我们可以键入

Pure\Config::path("app/Config");
// application's code...
$one = Pure\Config::get('app.one');
$name = Pure\Config::get('app.name');

其中first.name的语法表示

  • first指的是ini文件名
  • second指的是配置文件内的参数

除了命名完整的命名空间,还有一个别名函数config

Pure\Config::get('app.one') == config('app.one')

可以从不在基本路径中的ini文件中加载配置,在这种情况下指定路径

Pure\Config::get('other.one', 'app/OtherPath') == config('other.one', 'app/OtherPath')

身份验证

Pure提供了一个简单的身份验证接口,让您轻松管理用户和会话。

首先定义一个用户模型

<?php

namespace App\Schemas;
use Pure\SchemaHandler;
use App\Models\User;

class UserSchema extends SchemaHandler
{
    public function table(){
        return User::table();
    }

    protected function define($schema){
        $schema->add('id', 'INT');
        $schema->add('email', 'VARCHAR(30)', 'NOT NULL');
        $schema->add('password', 'VARCHAR(30)', 'NOT NULL');
        $schema->unique('email'); // email must be unique
        $schema->increments('id'); // auto_increment
        $schema->primary('id'); // set the primary key
        $schema->add('active', 'BOOLEAN');
        $schema->add('role', 'INTEGER');
    }
}

?>

Auth接口需要了解User模型

Pure\Auth::$class_name = Pure\Config::get(\App\Models\User::class);

一旦User模型类注册到Auth接口,就可以使用所有功能

// the check method returns true if the user is logged in
$is_logged_in = Pure\Auth::check();

// the authenticate method let the user to login
// authenticate($condition, $remember = false)
if(Pure\Auth::authenticate("email = '$email' AND password = '$password'"))
{
    // authenticated
}
else 
{
    // authentication failed
}

// The user method let to retireve the user model
$user_model = Pure\Auth::user();

// call logout to end the user session
Pure\Auth::logout();

模式生成

SchemaHandler是一个简单的接口,允许在应用程序启动时自动生成模式。

<?php

namespace App\Schemas;
use Pure\SchemaHandler;

class ExampleSchema extends SchemaHandler
{
    public function table(){
        return "schema_name";
    }

    protected function define($schema){
        $schema->add('id', 'INT');
        $schema->increments('id'); // auto_increment
        $schema->primary('id'); // set the primary key
        // other schema fields... 
    }
}

?>

让我们举个例子,这个模式处理器生成用户的模式

<?php

namespace App\Schemas;
use Pure\SchemaHandler;
use App\Models\User;

class UserSchema extends SchemaHandler
{
    public function table(){
        return User::table();
    }

    protected function define($schema){
        $schema->add('id', 'INT');
        $schema->add('email', 'VARCHAR(30)', 'NOT NULL');
        $schema->add('password', 'VARCHAR(30)', 'NOT NULL');
        $schema->unique('email'); // email must be unique
        $schema->increments('id'); // auto_increment
        $schema->primary('id'); // set the primary key
        $schema->add('active', 'BOOLEAN');
        $schema->add('role', 'INTEGER');
    }
}

?>

HTTP请求

Request类可用于访问HTTP参数。

对于GET HTTP请求,使用get方法

// url = localhost:8000/show_posts&category=books
$category = Pure\Request::get('category'); // equals books

对于POST HTTP请求,使用post方法

$id = Pure\Request::post('id');

使用input方法让应用程序根据请求类型自动使用get或post

$param = Pure\Request::input('param');

除了命名完整的命名空间,还有别名函数

Pure\Request::get('param') == get('param');
Pure\Request::post('param') == post('param');
Pure\Request::input('param') == request('param');

应用程序

待办事项

快速开发API

待办事项