dscdttg4389
2015-10-17 09:04
浏览 80
已采纳

Localhost中的Laravel 5.1 SSL

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

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

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

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

});

图片转代码服务由CSDN问答提供 功能建议

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

购物车,orderAddress,orderPayment,orderResults是订单处理的四个步骤。

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

如果我的好(在下面)“如何缩短此代码?” 因为我重复自己这就是我不喜欢的原因。

  / ** 
 *在Localhost中禁用ssl 
 * / 
if(App :: environment(  'local')){
 Route :: get('/ cart',[
'使用'=>'CartController @ index',
'as'=>'cart',
]);  
 Route :: get('/ orderAddress',[
'使用'=>'AddressController @ orderIndex',
'为'=>'orderAddress',
]); 
 Route :: get  ('/ orderPayment',[
'使用'=>'PaymentController @ orderPayment',
'作为'=>'orderPayment',
]); 
 Route :: get('/ orderResult',  [
'使用'=>'OrderController @ orderResult',
'为'=>'orderResult',
]); 
} else {
 / ** 
 * SSL PAGES 
 *  / 
Route :: group(['before'=>'force.ssl'],function()
 {
 Route :: get('/ cart',[
'使用'=>'CartController  @index',
'as'=>'cart',
]); 
 Route :: get('/ orderAddress',[
'使用'=>'AddressController @ orderIndex',
  'as'=>'orderAddress',
  ]); 
 Route :: get('/ orderPayment',[
'使用'=>  'PaymentController @ orderPayment',
'as'=>  'orderPayment',
]); 
 Route :: get('/ orderResult',[
'使用'=>'OrderController @ orderResult',
'为'=>'orderResult',
  ]); 
}); 
 
Route :: filter('force.ssl',function()
 {
 if if(!Request :: secure())
 {
返回Redirect :: secure  (Request :: path()); 
} 
 
}); 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanbarong4321 2015-10-17 09:57
    已采纳

    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

    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! :)

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题