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';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。