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

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

});
  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?