如何仅在laravel

时间:2017-01-25 11:44:03

标签: laravel laravel-5 laravel-5.1

我想将特定页面仅重定向到https,其余页面将保留在正常的http。

我想仅在付款页面执行此操作。成功的付款网站将以正常的http运行。

所以请帮我做这个。

我已经尝试过这个。

Route::resource('paynow', ['uses' => 'Account\PaymentController', 'https' => true]); 

但这对我不起作用。

1 个答案:

答案 0 :(得分:2)

我会通过在 app \ http \ middleware 中创建自定义中间件来拦截请求,然后才能拦截请求。

    <?php

namespace App\Http\Middleware;

use Closure;

class SecurePayment
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (!$request->secure()) {
            return redirect()->secure($request->getRequestUri());
        }

        return $next($request);
    }
}

然后将其添加到路由中间件组中的 app / http / kernel.php

/**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        .....

        'secure-payment' => \App\Http\Middleware\SecurePayment::class,
    ];

最后将你的路线包裹在小组

Route::group(['middleware' => ['secure-payment']], function() {
   Route::resource('paynow', ['uses' => 'Account\PaymentController']);
}):