laravel 不使用 Artisan 的维护模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21047573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:51:40  来源:igfitidea点击:

Maintenance Mode without using Artisan?

laravellaravel-4laravel-5

提问by Steve Bauman

I'm just wondering if anyone know's if there's a way to activate maintenance mode on a laravel website without using Artisan? I don't have command line access to the server so I can't use Artisan without first updating it on my local site and then push the changes to the server. Is there maybe a master Route that I can add that will deny access to any other routes?

我只是想知道是否有人知道是否有办法在不使用 Artisan 的情况下在 laravel 网站上激活维护模式?我没有对服务器的命令行访问权限,所以如果不先在我的本地站点上更新它,然后将更改推送到服务器,我就无法使用 Artisan。是否有我可以添加的主路由将拒绝访问任何其他路由?

Thanks!

谢谢!

回答by Antonio Carlos Ribeiro

You can just call artisan from your application:

您可以从您的应用程序中调用 artisan:

Artisan::call('down');

Artisan::call('up');

But since you'll not have access to get your app up because it's down. You can create the functionality yourself:

但是因为您将无法启动您的应用程序,因为它已关闭。您可以自己创建功能:

A route for shut it down, user must be authenticated to do this:

关闭它的路由,用户必须经过身份验证才能执行此操作:

Route::group(array('before' => 'auth'), function()
{

    Route::get('shut/the/application/down', function() 
    {
        touch(storage_path().'/meta/my.down');
    });

});

A route to bring it back up:

将其恢复的路线:

Route::get('bring/the/application/back/up', function() 
{
    @unlink(storage_path().'/meta/my.down');
});

A filter to check if it's up:

一个过滤器来检查它是否已启动:

Route::filter('applicationIsUp', function()
{
    if (file_exists($this['path.storage'].'/meta/my.down'))
    {
        return Redirect::to('site/is/down');
    }
});

A route to bring it back up:

将其恢复的路线:

Route::get('bring/the/application/back/up', function() 
{
    @unlink(storage_path().'/meta/my.down');
});

A route to show a pretty view when your site is down

当您的网站关闭时显示漂亮视图的路线

Route::get('site/is/down', function() 
{
    return View::make('views.site.down');
});

回答by SamV

This is for Laravel 4.1 and below

这适用于 Laravel 4.1 及以下版本

Laravel 5 downfile is in storage/framework/down- thanks @ruuter.

Laravel 5 下载文件已发布storage/framework/down- 感谢 @ruuter。

Taking a look at the DownCommandclass for Artisan, it seems to create a new file within the app/storage/metafolder.

看看DownCommandArtisan的类,它似乎在app/storage/meta文件夹中创建了一个新文件。

Heres the DownCommandfiremethod.

继承人的DownCommandfire方法。

public function fire()
{
    touch($this->laravel['path.storage'].'/meta/down');

    $this->comment('Application is now in maintenance mode.');
}

And the corresponding UpCommandfiremethod.

以及相应的UpCommandfire方法。

public function fire()
{
    @unlink($this->laravel['path.storage'].'/meta/down');

    $this->info('Application is now live.');
}

These files are located in vendor/laravel/framework/src/Illuminate/Foundation/Console.

这些文件位于vendor/laravel/framework/src/Illuminate/Foundation/Console.

It specifically creates a file called downin app/storage/meta.

它专门创建了一个名为文件downapp/storage/meta

You could create an authorized route / controller action to replicate these commands.

您可以创建一个授权的路由/控制器操作来复制这些命令。

Note Sjaak Trekhaa's comment below, that is the method I would use now that I've been reminded of it!

请注意下面 Sjaak Trekhaa 的评论,这就是我现在被提醒使用的方法!

回答by Captain Hypertext

Laravel 4

Laravel 4

Just wanted to put this out there for everyone on the web, all php artisan downdoes is touch (create) an empty file in the app/storage/metadirectory called 'down'. If this file exists, then the application is in maintenance mode. That's all there is to it:

只是想将它发布给网络上的每个人,php artisan down所做的就是在app/storage/meta名为“down”的目录中触摸(创建)一个空文件。如果此文件存在,则应用程序处于维护模式。这里的所有都是它的:

// From vendor\laravel\framework\src\Illuminate\Foundation\Application.php

public function isDownForMaintenance()
{
    return file_exists($this['config']['app.manifest'].'/down');
}

So if you can upload files, all you need to do is upload an empty file named 'down' to app/storage/meta.

因此,如果您可以上传文件,您只需上传一个名为“down”的空文件到app/storage/meta.



Laravel 5:

Laravel 5:

Down is located in storage/framework/down

下来位于 storage/framework/down

Thanks ruuter.

谢谢鲁特。

回答by Kyslik

The real correct answer to question is above provided by Antonio.

上面问题的真正正确答案是由安东尼奥提供的。

You can just call artisan from your application:

Artisan::call('down');

Artisan::call('up');

您可以从您的应用程序中调用 artisan:

Artisan::call('down');

Artisan::call('up');

Laravel 5+

Laravel 5+

Since middleware(s) were introduced in Laravel 5, I will cover how I do it in Laravel 5.3 application.

由于中间件是在 Laravel 5 中引入的,我将介绍我如何在 Laravel 5.3 应用程序中做到这一点。

Create brand new middleware

创建全新的中间件

First lets create new middleware $php artisan make:middleware CheckForMaintenanceMode

首先让我们创建新的中间件 $php artisan make:middleware CheckForMaintenanceMode

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Http\Exceptions\MaintenanceModeException;

class CheckForMaintenanceMode
{
    protected $app;

    public function __construct(Application $app)
    {
        $this->app = $app;
    }

    public function handle($request, Closure $next)
    {
        if ($this->app->isDownForMaintenance() && !$this->isBackendRequest($request)) {
            $data = json_decode(file_get_contents($this->app->storagePath() . '/framework/down'), true);

            throw new MaintenanceModeException($data['time'], $data['retry'], $data['message']);
        }

        return $next($request);
    }

    private function isBackendRequest($request)
    {
        return ($request->is('admin/*') or $request->is('login'));
    }
}

Note: function isBackendRequest()which returns trueor falseif we are either in admin prefix(true) or trying to login (true) or anything else (false)

注意isBackendRequest()返回的函数true或者false如果我们处于管理员前缀( true) 或尝试登录 ( true) 或其他任何东西 ( false)

replace global middleware

替换全局中间件

Open up App/Http/Kernel.phpand rewrite foundations middleware with our new middleware

App/Http/Kernel.php使用我们的新中间件打开和重写基础中间件

protected $middleware = [
    \App\Http\Middleware\CheckForMaintenanceMode::class,
];


If application is in maintenance mode (down) we can still access login page or any admin/*page.

如果应用程序处于维护模式(关闭),我们仍然可以访问登录页面或任何admin/*页面。

Route::group(['middleware' => 'auth', 'prefix' => 'admin'], function () { 
    //admin routes
});  

回答by Debbie V

In laravel 5.6, the location of the "down" file has moved slightly.

在 laravel 5.6 中,“向下”文件的位置略有移动。

It's new location is:

它的新位置是:

./storage/framework/down

./存储/框架/向下

Laravel 5.6 has added some contents to the "down" file, although an empty file is still effective. You get a "503 Service Unavailable" page, by default.

Laravel 5.6 在“down”文件中添加了一些内容,尽管空文件仍然有效。默认情况下,您会看到“503 服务不可用”页面。

Simply remove the "down" file when you're finished.

完成后,只需删除“向下”文件即可。