【问题标题】:Laravel 5.1 SSL in Localhost本地主机中的 Laravel 5.1 SSL
【发布时间】:2016-01-16 01:12:40
【问题描述】:

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

cart、orderAddress、orderPayment、orderResults 是 Order Process 的四个步骤。

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

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

/**
 * 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());
    }

});

【问题讨论】:

  • 您可以将本地测试放入过滤器并始终应用它。
  • “我的第一个问题是在 localhost 中禁用 ssl 证书的最佳做法是什么?” – 这是正确的问题吗?我宁愿说,为您的本地测试域颁发自签名证书,并让您的测试浏览器接受一次。在您的开发设置中尽可能地模仿您的生产设置始终是一个好主意。
  • @CBroe 你是对的。但我现在有电子商务系统。我正在使用。现在我决定转换为 laravel 项目。我没有太多时间。而且我不知道测试域上的自签名证书。你能分享我一个我想了解它的链接吗?谢谢。
  • 这应该很容易研究,google.com/search?q=create+self-signed+ssl+certificate 以及如何为 SSL/HTTPS 设置本地服务器。

标签: php laravel ssl localhost


【解决方案1】:

你可以写一个像这样的中间件:

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);
    }
}

然后在Kernel.php注册

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

现在你可以说

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',
    ]);
});

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

【讨论】:

  • 完美运行。存在小问题。我编辑了你的帖子。如果您确认它工作得很好。
猜你喜欢
  • 2016-02-21
  • 1970-01-01
  • 2015-10-21
  • 2015-08-26
  • 1970-01-01
  • 1970-01-01
  • 2015-09-15
  • 2019-03-21
  • 1970-01-01
相关资源
最近更新 更多