mindedge/blade

此包已被废弃,不再维护。作者建议使用 mindedge/ioc 包。
此包的最新版本(3.0.0)没有可用的许可证信息。

一个独立的 blade 包,实现了微型 IoC 容器,并将 Illuminate\view 绑定到它上面。

3.0.0 2019-11-08 21:11 UTC

This package is auto-updated.

Last update: 2019-11-15 14:18:43 UTC


README

Latest Version Build Status Coverage Status

Mindedge Blade

Composer 包,提供 Laravel 的 IoC 容器,任何 Laravel 服务都可以绑定到它。此包目前提供以下服务:

  1. Illuminate\view - GitHub 链接Illuminate API 文档

    这是提供 Blade 引擎的服务。

  2. Illuminate\config - GitHub 链接Illuminate API 文档

    这是允许服务拥有专用、易于理解的配置文件的服务。

  3. Illuminate\database - GitHub 链接Illuminate API 文档

这是提供 Eloquent 等数据库服务的服务。

现在该包托管在 Packagist 上,这意味着添加仓库数组不再必要。

https://packagist.org.cn/packages/mindedge/blade

安装

您可以从命令行在根目录中使用以下 require 语句(假设已存在 composer.json)

composer require mindedge/blade

或者,只需将现有依赖项添加到 composer.json 中的 "require" 对象中

"require": {
    "mindedge/blade": "^2.0.0"
}

然后运行

composer install

配置

在您的应用程序根目录中,创建四个目录。我使用 "ROOTDIR" 表示应用程序根目录。

  1. ROOTDIR/bootrap
  2. ROOTDIR/config
  3. ROOTDIR/cache
  4. ROOTDIR/views

注意:'views' 目录将对应于您的应用程序保存其表示层文件的位置,并且是可配置的。您可以根据应用程序的需要命名/更改 views 目录。

最终结果可能如下所示

|...
|
|-config
|-boostrap
|-cache
|-views
|
|...

在新建的 bootstrap 目录中,创建一个名为 app.php 的单个文件,内容如下。

require_once __DIR__.'/../vendor/autoload.php';

use  Mindedge\Blade\Application;

$app = new Application(
    dirname(__DIR__)
);

$app->withFacades();

//$app->withEloquent();



//Nothing Below These Two Lines!
$app->boot();

return $app;

注意
如果您不打算使用视图、数据库或配置外观,您可以将 '$app->withFacades out' 注释掉。
要使用 Eloquent,只需将 '$app->withEloquent' 取消注释。如果您打算使用模型,请确保在 composer.json 中添加一个 psr4 条目到它们的目录位置。

此示例显示了自动加载脚本被包含,但只要 vendor/autoload.php 在项目中某个全局可访问的地方被包含,这也是一种可行的方法。

在新建的 config 目录中,创建一个名为 view.php 的单个文件,并放置以下启动配置

<?php

return [

    'paths' => [
        './views'
    ],

    'compiled' => './cache',

];

此配置文件配置了 View 库(Blade),并包含一个包含两个键的数组。

  1. 'paths' - 表示 Illuminate\view 服务应查找文件的位置。此目录接受的文件类型是 .php 和 .blade.php。您可以将任意位置添加到 paths 数组中。

  2. '编译' - 表示编译/解析后的视图应该放在哪里。如果一个给定的文件没有改变,视图将跳过编译步骤,并从这个文件夹中读取相应文件的编译内容。请确保这个文件夹可以被应用程序/网络用户写入。

添加另一个配置文件,用于数据库库(包括eloquent)。在配置目录中创建一个名为database.php的单个文件,并放置以下起始配置

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Database Connection Name
    |--------------------------------------------------------------------------
    |
    | Here you may specify which of the database connections below you wish
    | to use as your default connection for all database work. Of course
    | you may use many connections at once using the Database library.
    |
    */

    'default' => env('DB_CONNECTION', 'mysql'),

    /*
    |--------------------------------------------------------------------------
    | Database Connections
    |--------------------------------------------------------------------------
    |
    | Here are each of the database connections setup for your application.
    | Of course, examples of configuring each database platform that is
    | supported by Laravel is shown below to make development simple.
    |
    |
    | All database work in Laravel is done through the PHP PDO facilities
    | so make sure you have the driver for your particular database of
    | choice installed on your machine before you begin development.
    |
    */

    'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
            'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
        ],

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
        ],

        'pgsql' => [
            'driver' => 'pgsql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],

        'sqlsrv' => [
            'driver' => 'sqlsrv',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | Migration Repository Table
    |--------------------------------------------------------------------------
    |
    | This table keeps track of all the migrations that have already run for
    | your application. Using this information, we can determine which of
    | the migrations on disk haven't actually been run in the database.
    |
    */

    'migrations' => 'migrations',

    /*
    |--------------------------------------------------------------------------
    | Redis Databases
    |--------------------------------------------------------------------------
    |
    | Redis is an open source, fast, and advanced key-value store that also
    | provides a richer body of commands than a typical key-value system
    | such as APC or Memcached. Laravel makes it easy to dig right in.
    |
    */

    'redis' => [

        'client' => 'predis',

        'default' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => env('REDIS_DB', 0),
        ],

        'cache' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => env('REDIS_CACHE_DB', 1),
        ],

    ],

];

所有数据库配置值都应该定义在.env文件中,如果需要,在配置文件中定义一个localhost的默认备份。有关配置的更多信息,可以在官方Laravel文档网站找到

假设所有上述步骤都正确执行,你应该有一个类似以下结构的目录

|...
|
|-config
|   |
|   view.php
|   database.php
|-boostrap
|   |
|   app.php
|-cache
|-views
|   |
|   welcome.blade.php
|
|...

在应用程序的入口点中包含boostrap/app.php,在某个全局包含或自动加载的位置,现在你就可以使用这个包提供的所有三个服务了。

有关进一步的帮助和文档,请参阅以下链接

Laravel Blade 文档。

Laravel 数据库文档。

附加服务

  1. 服务提供者 - 此包允许您注册额外的服务并将它们绑定到IoC容器中。要注册服务,请在bootstrap/app文件中调用register方法。服务类文件和服务提供者文件必须位于psr4自动加载的位置。
//boostrap/app.php 

$app->register('Acme\Company\AcmeServiceProvider');

  1. Facades - 如果您为您自己的服务创建了一个门面,或者想要注册来自不同包的门面,您可以通过将引用传递给'withFacades'方法来做到这一点。您要注册门面的类必须位于psr4自动加载的目录中。
//bootstrap/app.php

$app->withFacades(true, ['Acme\Company\AcmeService' => 'AcmeService']);
  1. 配置文件 - 如果您创建了一个自定义服务,并想使用配置文件,或者正在使用使用配置文件的第三方包,您可以创建并注册它。在您的配置目录中,创建或导入配置文件。要注册配置到应用程序中,请使用configure方法。
//bootstrap/app.php

$app->configure('name_of_config_file');