本地主机中的 Laravel 5.1 SSL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33184775/
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 12:31:27  来源:igfitidea点击:

Laravel 5.1 SSL in Localhost

phplaravelssllocalhost

提问by Cihan Küsmez

I'm moving my working project to laravel. In order process i'm using SSL Certificate.

我正在将我的工作项目转移到 Laravel。在订单过程中,我正在使用 SSL 证书。

cart, orderAddress, orderPayment, orderResults are four steps of Order Process.

购物车、订单地址、订单付款、订单结果是订单流程的四个步骤。

My first question is "What is best practice to disable ssl certificate in localhost ?"

我的第一个问题是“在 localhost 中禁用 ssl 证书的最佳做法是什么?”

If mine is good (in below) "How can i shorten this code ?" Because i'm repeating myself that's why i didn't like.

如果我的是好的(在下面)“我怎样才能缩短这段代码?” 因为我在重复自己,这就是我不喜欢的原因。

/**
 * Disable ssl in Localhost
 */
if (App::environment('local')) {
    Route::get('/cart', [
        'uses' => 'CartController@index',
        'as'   => 'cart',
    ]);
    Route::get('/orderAddress', [
        'uses' => 'AddressController@orderIndex',
        'as'   => 'orderAddress',
    ]);
    Route::get('/orderPayment', [
        'uses' => 'PaymentController@orderPayment',
        'as'   => 'orderPayment',
    ]);
    Route::get('/orderResult', [
        'uses' => 'OrderController@orderResult',
        'as'   => 'orderResult',
    ]);
} else {
/**
 * SSL PAGES
 */
Route::group(['before' => 'force.ssl'], function()
{
    Route::get('/cart', [
        'uses' => 'CartController@index',
        'as'   => 'cart',
    ]);
    Route::get('/orderAddress', [
        'uses' => 'AddressController@orderIndex',
        'as'   => 'orderAddress',
    ]);
    Route::get('/orderPayment', [
        'uses' => 'PaymentController@orderPayment',
        'as'   => 'orderPayment',
    ]);
    Route::get('/orderResult', [
        'uses' => 'OrderController@orderResult',
        'as'   => 'orderResult',
    ]);
});

Route::filter('force.ssl', function()
{
    if( ! Request::secure())
    {
        return Redirect::secure(Request::path());
    }

});

回答by AliRNazari

You can write a middle ware like:

您可以编写一个中间件,例如:

namespace App\Http\Middleware;

use Closure;
use App;
use Redirect;

class UseSSL
{

    public function handle($request, Closure $next)
    {
        if( App::environment('local') ){
            return Redirect::secure($request->path());
        }

        return $next($request);
    }
}

Then register it in Kernel.php

然后在里面注册 Kernel.php

protected $routeMiddleware = [
    ...
    'use.ssl' => UseSSL::class
];

Now you can say

现在你可以说

Route::group(['middleware' => 'use.ssl'], function () {

    Route::get('/cart', [
        'uses' => 'CartController@index',
        'as'   => 'cart',
    ]);
    Route::get('/orderAddress', [
        'uses' => 'AddressController@orderIndex',
        'as'   => 'orderAddress',
    ]);
    Route::get('/orderPayment', [
        'uses' => 'PaymentController@orderPayment',
        'as'   => 'orderPayment',
    ]);
    Route::get('/orderResult', [
        'uses' => 'OrderController@orderResult',
        'as'   => 'orderResult',
    ]);
});

Let me know if it works! :)

让我知道它是否有效!:)