jmrg/chip-modules

构建PHP微框架的组件。

v1.0.0 2018-02-09 07:21 UTC

This package is not auto-updated.

Last update: 2024-10-02 04:04:39 UTC


README

Build Status

Chip-modules 是一系列库,封装了它们的实现方式,使其更加简单快捷,并提供了一步到位的访问方法。

组件

这包括以下处理程序

安装 Chip-modules Composer

您可以使用 Composer 将 Chip-modules 安装到项目中。对于现有应用程序,您可以运行以下命令

$ composer require jmrg/chip-modules:"~0"

使用说明

环境配置

为了配置环境参数,创建一个类似下面的 YAML 文件,包含数据库连接和 SMTP 参数

# There are file for example named example.environment.yaml

databases:
    prod:
        driver: mysql
        host: localhost
        database: database
        username: root
        password: secret
        charset: utf8
        collation: utf8_unicode_ci

mail:
    # 0 = off | 1 = Client messages | 2 = Client and server messages
    debug: 2

    # Basics
    driver: smtp
    host: smtp.mailtrap.io
    port: 2525
    username: null
    password: null

    # Encryption system: tls, ssl (deprecated)
    encryption: null

之后,我们应该加载该文件。

// Load file...
ConfigModule::init('path/to/file/config.yaml');

// Getting config...
$c = config(); // Return the config as array.

路由配置

要配置路由,我们执行以下操作

// Creates a new object to the configuration.
$config = new \Chip\Modules\Router\RouterConfig();
$config->setBaseNamespace("\\Namespace\\to\\controllers") // Namespace source path.
    ->attachRouterFiles('path/to/endpoints/router.php'); // Location for the file router.

// Dispatch resquest.
$r = \Chip\Modules\Router\Router::up($config);
$r->dispatch();

使用路由的示例

// File router.php

// Methods availables according to HTTP verbs: get(), post(), put(), pat(), delete(), options().
$router->get('/hellow-world', function () {
    return 'Hellow World!!';
});

// To response multiple HTTP verbs:
$router->match(['get', 'post'], '/hellow-world', function () {
    return 'Hellow World!!';
});

数据库和模型配置

要配置数据库,我们执行以下操作

// In the file configuration environment, we have a sections with the
// connections to the different databases, we only must send
// a connections name to configure de database.

\Chip\Modules\Model\Manager::db('nameDatabase');

// In the case of models, these must extend from \Chip\Modules\Model\Model
class User extend \Chip\Modules\Model\Model
{
    // Something code..
}

会话配置

要配置会话,我们执行以下操作

// Recomended to use name "guest" for the session.
\Chip\Modules\Session\Session::of('Guest'); 

// To get the session to use a guest() method.
// This returns an instance of Hoa Session. Check out the documentation
// of lirary for any information https://github.com/hoaproject/Session
$session = guest();

视图和 Blade 配置

要配置 blade 管理器模板,我们执行以下操作

// Define path to views and cache.
ViewComponent::config(
    '/path/to/source/views',
    '/path/to/cache'
);

// Load the view...
view(
    'name-view',
    ['param1' => 'Hello', 'param2' => 'World!!']
);

// To build views check out the documentation https://laravel.net.cn/docs/5.1/blade

邮件配置

要配置第一项,我们首先在环境文件中填写所需的参数

# There are file for example named example.environment.yaml

mail:
    # 0 = off | 1 = Client messages | 2 = Client and server messages
    debug: 2

    # Basics
    driver: smtp
    host: smtp.mailtrap.io
    port: 2525
    username: null
    password: null

    # Encryption system: tls, ssl (deprecated)
    encryption: null

要获取邮件处理程序的实例,请执行以下操作

// Create a new isntance Mailer class.
$mailer = \Chip\Modules\Mailer\Mailer::create();

// To send emails or making config aditionals check out the documentation https://github.com/PHPMailer/PHPMailer

测试

此项目使用 PHPUnit 进行测试。要执行测试,请运行以下命令

$ vendor/bin/phpunit