aedart/laravel-application

此包已被废弃且不再维护。作者建议使用 aedart/laravel-helpers 包。

为 Laravel 应用实例提供的 Getter 和 Setter 包。它作为框架基础助手文件中 app() 方法的替代。

1.1.0 2015-06-21 17:19 UTC

This package is auto-updated.

Last update: 2022-02-01 12:46:15 UTC


README

为 Laravel 应用实例提供的 Getter 和 Setter 包。它作为框架基础助手文件中 app() 方法的替代。

内容

[目录]

何时使用此包

当你的组件需要知道 Laravel 应用实例时

如何安装

对于 Laravel 版本 5.0.x

#!console

composer require aedart/laravel-application 1.0.*

此包使用 composer。如果您不知道它是做什么的或者如何使用,我建议您在使用此包之前先了解一下。

快速开始

假设您有一个接口,例如用于命令,您可以扩展应用感知接口;

#!php
<?php
use Aedart\Laravel\Application\Interfaces\ApplicationAware;

interface ICommand extends ApplicationAware {

    // ... Remaining interface implementation not shown ...
    
}

在您的具体实现中,您只需使用应用特质;

#!php
<?php
use Aedart\Laravel\Application\Traits\ApplicationTrait;

class DoSomethingCommand implements ICommand {
 
    use ApplicationTrait;

    // ... Remaining implementation not shown ... 
 
}

默认应用实例

ApplicationTrait 默认将返回当前运行的 Laravel 应用实例(如果有的话)。然而,如果没有可用的应用实例,则返回 null

在尝试使用应用实例之前,请确保应用可用,请使用 hasApplication()hasDefaultApplication() 方法。

默认应用验证

默认情况下,ApplicationTrait 总是假设指定的应用实例是有效的。但是,如果您需要确保特定的配置已被设置或某些服务提供者可用,或者进行其他类型的应用特定验证,则可以通过覆盖 isApplicationValid() 方法来实现。

#!php
<?php
use Aedart\Laravel\Application\Traits\ApplicationTrait;

class DoSomethingCommand implements ICommand {
 
    use ApplicationTrait;

    public function isApplicationValid(Application $application){
        // In this example, we ensure that the database default driver
        // is sqlite
        $defaultDriver = $application['config']['database.default'];
        if($defaultDriver == 'sqlite'){
            return true;
        }
        
        return false;
    }

    // ... Remaining implementation not shown ... 
 
}

致谢

Taylor Otwell 等人,感谢他们创造了最好的 PHP 框架之一。

许可证

BSD-3-Clause,阅读此包中包含的 LICENSE 文件