php developer
php developer

Reputation: 185

how to redirect with https for particular page only in laravel

i want to redirect particular page only to https, rest of page will remain in normal http.

i want to do this for payment page only.after successful payment site will run with normal http.

so please help me for do this.

i already try this one.

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

but this will not work for me.

Upvotes: 0

Views: 232

Answers (1)

Rob Fonseca
Rob Fonseca

Reputation: 3819

I would go about it by creating a custom middleware in app\http\middleware to intercept the request before it hits those routes.

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

Then add it to app/http/kernel.php in the route middleware group

/**
     * 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,
    ];

and finally wrap your route in the group

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

Upvotes: 2

Related Questions