messagebooster / uflex

一站式PHP用户认证类

1.0.2 2014-06-26 03:09 UTC

This package is not auto-updated.

Last update: 2024-09-24 07:30:25 UTC


README

这是一个简单的PHP用户认证库。此库在PHP 5.3.x环境下开发、维护和测试。单元测试也在Travis-CI上运行,支持PHP 5.4.x和PHP 5.5.x。

单个类文件 class.uFlex.php 的代码可以在旧分支上找到

Build Status

更多信息

从旧版本升级...

在更新之前,您需要运行一个SQL升级脚本。在运行升级脚本之前,请确保备份您的数据库。请参考DB目录https://github.com/ptejada/uFlex/tree/master/db

如果不使用Composer,而是直接包含PHP类,您需要在应用程序中包含autoload.php脚本,该脚本将按需自动包含库类。

如果使用composer,只需在应用程序中包含vendor/autoload.php即可,如果尚未包含。

总体而言,版本1.0采用更面向对象的编程方法,并更紧密地遵循约定。有关更多信息,请参阅API更改

入门

将其包含到项目中

如果您使用Composer,只需将ptejada/uflex作为依赖项添加。注意uflex的大小写,全部小写。例如

{
    "require": {
        "ptejada/uflex": "1.*"
    }
}

当使用Composer时,使用vendor/autoload.php脚本将库包含到您的项目中。

如果您不使用Composer,则可以在项目中克隆此存储库。使用autoload.php脚本将库包含到您的项目中。

配置用户对象

User类实例化时,并不会发生太多事情,会话不会被初始化,也不会建立数据库连接。这是为了允许配置类。一旦配置完成,必须调用start()方法,以便开始用户认证过程。例如

<?php
    include('/path/to/uflex/directory/autoload.php');

    //Instantiate the User object
    $user = new ptejada\uFlex\User();

    //Add database credentials
    $user->config->database->host = 'localhost';
    $user->config->database->user = 'test';
    $user->config->database->password = 'test';
    $user->config->database->name = 'uflex_test'; //Database name

    /*
     * You can update any customizable property of the class before starting the object
     * construction process
     */

    //Start object construction
    $user->start();
?>

最好为每个项目创建一个像上面这样的配置文件。这样,您可以使用配置文件为项目中的任何PHP脚本提供预配置的用户实例。

或者,您可以创建自己的类,该类为您配置并启动User对象。例如

<?php
    
    class MyUser extends ptejada\uFlex\User {
        public function __construct()
        {
            parent::__construct();
            
            //Add database credentials
            $this->config->database->host = 'localhost';
            $this->config->database->user = 'test';
            $this->config->database->password = 'test';
            $this->config->database->name = 'uflex_test'; //Database name

            // Start object construction
            $this->start();
        }
    }
?>

以下是PHP类文件的一部分,列出了您在调用User实例上的start()之前可以更改的自定义config属性。注意:config属性是一个Collection实例

	'cookieTime'      => '30',
    'cookieName'      => 'auto',
    'cookiePath'      => '/',
    'cookieHost'      => false,
    'userTableName'   => 'users',
    'userSession'     => 'userData',
    'userDefaultData' => array(
        'Username' => 'Guess',
        'ID'  => 0,
        'Password' => 0,
    ),
    'database' => array(
        'host'     => 'localhost',
        'name'     => '',
        'user'     => '',
        'password' => '',
        'dsn'      => '',
    )

理解集合

一个 Collection 是数组的对象表示。在项目中,Collection 有很多用途,并且易于使用。Collection 为我们处理未定义索引的错误并简化代码。

考虑以下使用普通数组的示例

<?php
    $data = array(
        'name' => 'pablo',
    );

    if (isset($data['quote']) && $data['quote'])
    {
        echo $data['name'] . "'s quote is: " . $data['quote'];
    }
    else
    {
        echo $data['name'] . " has no quote";
    }
?>

下面是使用 Collection 的相同代码

<?php
    $data = ptejada\uFlex\Collection(array(
        'name' => 'pablo',
    ));

    if ($data->quote)
    {
        echo  "{$data->name}'s quote is:  $data->quote";
    }
    else
    {
        echo "{$data->name} has no quote";
    }
?>

有关 Collection 类的更多信息,请参阅 API 文档

使用会话

User 对象通过其 session 属性提供对 PHP 会话的简单管理,该属性是 Session 类的一个实例。默认情况下,User 类管理 PHP 超全局变量 $_SESSION 中的 userData 命名空间,但可以在启动 User 对象之前通过设置 config->userSession 来配置。这非常强大,因为它允许 User 类使用 PHP 会话而不干扰其他软件的会话使用。

Session 类仅是扩展了 Collection,因此它像任何其他集合一样工作。唯一的不同之处在于几个额外的方法以及它是一个链接集合,这意味着在对象中进行的任何更改都会反映在 $_SESSION 上,从而自动保存到 PHP 会话中。

以下代码及其输出将更好地说明如何将所有内容组合在一起

<?php

    $user = new ptejada\uFlex\User();

    // Change the session namespace
    $user->config->userSession = 'myUser';

    $user->start();

    // Shows session right after the User object has been started
    print_r($_SESSION);

    // Stores something in the session
    $user->session->myThing = 'my thing goes here';

    // Shows the session with the content of the myThing property
    print_r($_SESSION);

    // Stores something in the PHP session outside of the namespace scope managed by the User class
    $_SESSION['other'] = 'other information stored here';

    print_r($_SESSION);

    // Only destroys the session namespace managed by the User Class
    $user->session->destroy();

    print_r($_SESSION);

?>

以下是上一段代码的输出

Array
(
    [myUser] => Array
        (
            [data] => Array
                (
                    [Username] => Guess
                    [ID] => 0
                    [Password] => 0
                )

        )
)

Array
(
    [myUser] => Array
        (
            [data] => Array
                (
                    [Username] => Guess
                    [ID] => 0
                    [Password] => 0
                )

            [myThing] => my thing goes here
        )
)

Array
(
    [myUser] => Array
        (
            [data] => Array
                (
                    [Username] => Guess
                    [ID] => 0
                    [Password] => 0
                )

            [myThing] => my thing goes here
        )

    [other] => other information stored here
)

Array
(
    [other] => other information stored here
)

Session 类还可以用于应用程序的其他方面。例如,要管理整个 PHP 会话,可以实例化 Session 类而不传递任何参数:new ptejada\uFlex\Session()

有关 Session 类的更多信息,请参阅 API 文档

扩展用户类

在 PHP 中,你可以像在任何面向对象编程语言中一样扩展类。因此,你可以通过添加自己的方法或修改而不必修改类文件本身来扩展 User 类的功能。你只需创建一个新的 PHP 类,该类扩展 User 类即可。

<php
	class User extends ptejada\uFlex\User{
		/*
		 * Add your default properties values
		 * such as database connection credentials
		 * user default information
		 * Or cookie preferences
		 */

		/*
		 * Create your own methods
		 */
		function updateAvatar(){}

		function linkOpeniD(){}
	}
?>