douyin6188 2017-10-17 10:28
浏览 355
已采纳

Laravel / Lumen核心应用中的覆盖方法

Is it possible to override a function defined in the Laravel/Lumen Application class?

For example, this is the definition of isDownForMaintenance in the Lumen Application class:

public function isDownForMaintenance() : bool
{
    return false;
}

I would like to override this with my own implementation like so:

public function isDownForMaintenance() : bool
{
    // Do something…
}

I have tried…

AppServiceProvider.php

$this->app->extend(‘app’, function () {
    return new Application; // Extension of Laravel/Lumen/Application
});

Application.php

class Application extends BaseApplication
{
    public function isDownForMaintenance() : bool
    {
        // Do Something…
    }
}
  • 写回答

1条回答 默认 最新

  • douliang6563 2017-10-17 13:33
    关注

    After exploring throughout the web, I have managed to stumble upon an article that outlines exactly what I was looking for. For the purposes of simplicity, I will outline how to extend Laravel's & Lumen's core Application class, but for those of you who wish to see a more in depth description, please see here:

    https://mattstauffer.com/blog/extending-laravels-application/

    This is surprisingly, extremely easy... First, we find the place where Application is created i.e. /bootstrap/app.php

    Then, we find the following line:

    Laravel

    $app = new Illuminate\Foundation\Application(
        realpath(__DIR__.'/../')
    );
    

    Lumen

    $app = new Laravel\Lumen\Application(
        realpath(__DIR__.'/../')
    );
    

    And then quite simply change to this:

    $app = new Custom\Application(
        realpath(__DIR__.'/../')
    );
    

    You can then do whatever you like with `Custom\Application', for instance;

    class Application extends BaseApplication
    {
        // Override the maintenance mode detection...
        public function isDownForMaintenance() : bool
        {
            // Do Something…
        }
    
        // Override the default storage path...
        public function storagePath()
        {
            return $this->basePath.'/custom/storage';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误