How To Redirect To HTTPS Using Laravel Middleware

How To Redirect To HTTPS Using Laravel Middleware

I believe this is not your first time of hearing about HTTP & HTTPS. These two are transfer protocols for the web.

I believe this is not your first time of hearing about HTTP & HTTPS. These two are transfer protocols for the web.

There are lots of articles out there that explains why HTTPS is highly recommended but I will summarize it this way: HTTPS is a secured version of HTTP. So if you care about the security of your web application you need to care about HTTPS.

Laravel Middleware

According to Laravel documentation, middleware provide a convenient mechanism for filtering HTTP requests entering your application.

So there are lots of things that you can do with middleware but for this tutorial we are going to check if any HTTP request is entering our application and them force it to use HTTPS.

I am assuming you already have a Laravel app where you want to apply this.

Note: You need to install SSL certificate (like LetsEncrypt) on the server running your app before you continue with this tutorial!

Lets Start!

Disclaimer: This is just a work around I use to avoid going through the inconvenience that .htaccess file can bring.

Create your SecuredHttp middleware class using this command:

php artisan make:middleware SecuredHttp

All Laravel middleware can be found on this path: app\Http\Middleware

Now lets add our redirection logic inside our middleware . All HTTP requests coming into Laravel middleware are handled inside the handle() method.

public function handle($request, Closure $next){
  if (!$request->secure()) {
      return redirect()->secure($request->path());
  }
  return $next($request);
}

Our logic is pretty simple. It checks for unsecured request and redirect it back to the same path that is secured (HTTPS).

Now we are going to register our SecuredHttp middleware class as a global middleware. This will allow it to run on every HTTP request coming into our application.

Update $middleware property of your app/Http/Kernel.php class like this:

protected $middleware = [
   ....
   \App\Http\Middleware\SecuredHttp::class,
   ....
];

Now your SecuredHttp middleware is actively waiting for HTTP requests that will near your app :)

Conclusion

I just showed you one out of a million things you can do with Laravel middleware.

Top Laravel Development Companies Reviews

Top Laravel Development Companies Reviews

Among the list of Best Laravel development companies, TopDevelopers has chosen the most acclaimed laravel developers for the visitors so as to make them avail the extremely wonderful service ever offered. The web development companies listed here...

Among the list of Best Laravel development companies, TopDevelopers has chosen the most acclaimed laravel developers for the visitors so as to make them avail the extremely wonderful service ever offered. The web development companies listed here have passed the stringent assessments carried out by TopDevelopers in order to get approved as top Laravel service providers. Get in touch with the most sought-after Laravel developers in the market for your assistance.

Here is the List of Best Laravel Development Companies.

How to Redirect to HTTPS using Laravel Middleware

How to Redirect to HTTPS using Laravel Middleware

This Laravel tutorial explains how to redirect to HTTPS using Laravel Middleware. I believe this is not your first time of hearing about HTTP & HTTPS. So there are lots of things that you can do with Laravel middleware but for this tutorial we are going to check if any HTTP request is entering our application and them force it to use HTTPS.

I believe this is not your first time of hearing about HTTP & HTTPS. These two are transfer protocols for the web.

There are lots of articles out there that explains why HTTPS is highly recommended but I will summarize it this way: HTTPS is a secured version of HTTP. So if you care about the security of your web application you need to care about HTTPS.

Laravel Middleware

According to Laravel documentation, middleware provide a convenient mechanism for filtering HTTP requests entering your application.

So there are lots of things that you can do with middleware but for this tutorial we are going to check if any HTTP request is entering our application and them force it to use HTTPS.

I am assuming you already have a Laravel app where you want to apply this.

Note: You need to install SSL certificate (like LetsEncrypt) on the server running your app before you continue with this tutorial!

Lets Start!

Disclaimer: This is just a work around I use to avoid going through the inconvenience that .htaccess file can bring.

Create your SecuredHttp middleware class using this command:

php artisan make:middleware SecuredHttp

All Laravel middleware can be found on this path: app\Http\Middleware

Now lets add our redirection logic inside our middleware . All HTTP requests coming into Laravel middleware are handled inside the handle() method.

public function handle($request, Closure $next){
  if (!$request->secure()) {
      return redirect()->secure($request->path());
  }
  return $next($request);
}

Our logic is pretty simple. It checks for unsecured request and redirect it back to the same path that is secured (HTTPS).

Now we are going to register our SecuredHttp middleware class as a global middleware. This will allow it to run on every HTTP request coming into our application.

Update $middleware property of your app/Http/Kernel.php class like this:

protected $middleware = [
   ....
   \App\Http\Middleware\SecuredHttp::class,
   ....
];

Now your SecuredHttp middleware is actively waiting for HTTP requests that will near your app :)

Conclusion

I just showed you one out of a million things you can do with Laravel middleware.

How to use Laravel 6 Guzzle Http Client Request?

How to use Laravel 6 Guzzle Http Client Request?

Do you know how to use Laravel 6 Guzzle http client request? I will say did you used before Guzzle 6 with Laravel? If no than i will guide you how to use Guzzle http client GET and POST request with PHP Laravel 6

We will use guzzlehttp/guzzle composer package for guzzle http request in Laravel 6 Application.

We can make simply http request with json data or multipart form data, also you can set header of request in guzzlehttp Laravel 6.

A Guzzle is a PHP HTTP client that makes it easy to send HTTP requests with data, headers and trivial to integrate with web services. Guzzle is a simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc.

Install Package:

Now we will install guzzlehttp/guzzle package and then we can easily use thir method So let's just run bellow command.

composer require guzzlehttp/guzzle
Example of Requests Using Guzzle:

Now here i will show you how to run all above listed request you can use following controller method:

GET Request:

public function getGuzzleRequest()

{

    $client = new \GuzzleHttp\Client();

    $request = $client->get('http://myexample.com');

    $response = $request->getBody();

   

    dd($response);

}

POST Request:

public function postGuzzleRequest()

{

    $client = new \GuzzleHttp\Client();

    $url = "http://myexample.com/api/posts";

   

    $myBody['name'] = "Demo";

    $request = $client->post($url,  ['body'=>$myBody]);

    $response = $request->send();

  

    dd($response);

}

PUT Request:

public function putGuzzleRequest()

{

    $client = new \GuzzleHttp\Client();

    $url = "http://myexample.com/api/posts/1";

    $myBody['name'] = "Demo";

    $request = $client->put($url,  ['body'=>$myBody]);

    $response = $request->send();

   

    dd($response);

}

DELETE Request:

public function deleteGuzzleRequest()

{

    $client = new \GuzzleHttp\Client();

    $url = "http://myexample.com/api/posts/1";

    $request = $client->delete($url);

    $response = $request->send();

  

    dd($response);

}

As above example, you can see how it works.

I hope it can help you...