简体   繁体   English

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

[英]Override Method in Laravel/Lumen Core Application

Is it possible to override a function defined in the Laravel/Lumen Application class? 是否可以覆盖Laravel / Lumen Application类中定义的函数?

For example, this is the definition of isDownForMaintenance in the Lumen Application class: 例如,这是Lumen Application类中isDownForMaintenance的定义:

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 AppServiceProvider.php

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

Application.php Application.php

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

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: 为了简单起见,我将概述如何扩展Laravel和Lumen的核心Application类,但是对于那些希望了解更详细说明的人,请参见此处:

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

This is surprisingly, extremely easy... First, we find the place where Application is created ie /bootstrap/app.php 这非常令人惊讶,非常容易...首先,我们找到创建Application的位置,即/bootstrap/app.php

Then, we find the following line: 然后,我们找到以下行:

Laravel 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';
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM