Ben Lobaugh Online

I think, therefore I am. I am, therefore I sail

Page 2 of 162

Lock attached to door

How to Force SSL (HTTPS) in Laravel

Out of the box, Laravel will allow both HTTPS and HTTP requests to your website or application. Ideally all requests are served via SSL. There are several ways to accomplish this, some of which require access to server configs. I am going to show you how you can easily force SSL in your Laravel application to ensure HTTPS will be on no matter where you application is deployed. Of course the server still requires a valid SSL certificate to run- that is outside the scope of this tutorial.

Assumptions

  • Working server with the SSL certificate already installed
  • Laravel app running
  • Requests being fulfilled on both HTTP and HTTPS
  • Basic Laravel knowledge

Time to complete in your app

30 minutes or less!

How it works

Laravel provides a mechanism for filtering HTTP requests called Middleware. Middleware has many use cases, a few of which include: user verification, CORS headers, logging requests, etc. Only after all Middleware conditions have been met is the app able to fulfill the visitor’s request. This is the perfect place for us to enforce SSL.

Read more about Middleware in the on the official Laravel docs https://laravel.com/docs/master/middleware

Step One: Create the Middleware Base

Middleware can be generated automatically with artisan with the following command:

php artisan make:middleware ForceSSL

The new file will be created in app/Http/Middleware/ForceSSL.php

namespace App\Http\Middleware;

use Closure;

class ForceSSL
{

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        return $next($request);
    }
}

Step Two: Update the handler

All we have to do now is look at the request and if it is not secure redirect to the secure version of the url!

Update the handle method like so

    public function handle($request, Closure $next)
    {

        // If the request is not secure, redirect to the HTTPS url.
        if( !$request->secure() ) {
            return redirect()->secure( $request->getRequestUri() );
        }

        // Otherwise carry on.
        return $next($request);
    }

Step Three: Test

That is it. 

Really.

Go test it.

Step Four: Enjoy a nice scotch

‘nuff said

Photo by Anita Jankovic on Unsplash

Man reading newspaper

Add a Mailchimp email subscription form in Laravel – double opt in!

With several fantastic extension out there, integrating a Mailchimp email subscription form in Laravel has never been easier!

I am going to show you an example integration that collects an email address in a simple form. We will be using the laravel-newsletter package by spatie.

Assumptions

  • You have a Laravel application already running
  • You have a Mailchimp account
  • You have a basic understanding of Laravel

Step One: Include the spatie/laravel-newsletter package

Include the spatie/laravel-newsletter package in your project with

composer require spatie/laravel-newsletter

This package contains a very easy to use set of methods to control all aspects of your subscribers and user management. In this tutorial we will only be using the subscribe functionality.

Step Two: Configuration

The default configuration file will be generated with

php artisan vendor:publish --provider="Spatie\Newsletter\NewsletterServiceProvider"

A file will be created at config/newsletter.php. In this file are a set of variables that will be needed in the .env file.

Step Three: Create the Controller

A Controller will be used to send off the subscriber’s email to the Mailchimp service. Other tutorials on form submission will also include a Model and Migration here. I am choosing to skip that since we are not putting data into a database, but rather are acting as a passthrough.

php artisan make:controller Subscriber 

The controller file will be created at app/Http/Controllers/Subscriber.php

Add a method called addSubscriber

    public function addSubscriber( Request $request ) {

        $validatedData = $request->validate([
            'email' => 'required|email',
        ]);

        // Do additional validation and security checks here....

        // Sign up the user!
        Newsletter::subscribePending( $validatedData['email'] );

        if( Newsletter::lastActionSucceeded() ) {
            $status = json_encode( [ 'success' => true, 'message' => 'You have been added to the list! Please check your email to confirm.' ] );
        } else {
            $status = json_encode( [ 'success' => false, 'message' => 'There was an issue adding you to the list! Please try again or contact the admin.', 'error' => Newsletter::getLastError() ] );
        }

        return $status;
    }

Step Four: Update routes

Next you will need to update your routes file to add the new endpoint. Do this in routes/web.php

Route::get('subscriber/add’, 'Subscriber@addSubscriber');

If you are building this into an API you can use the routes/api.php file.

Step Five: Wire up your form

This is the final step to world domination!…. or at least having your subscription form ready for prime time. Simply wire up your form to use this new endpoint. I am going to assume that you know how to handle html forms here 😉

Step Six: Final Testing

Because we set this up as a GET query, testing is as simple as passing the email query parameter. Submit an email and see it populate in your Mailchimp account!

Step Seven: Drink Scotch

The final and most important step!

Wrong Way Sign

The #1 mistake leaders make when learning to coach their team

In the early years of my career I rose through the ranks pretty rapidly. I had an uncanny ability to see the big picture and work at both a high and low level. I was good, really good. Being good meant that peers began to rely heavily on my knowledge and skills. When there were questions I was who people turned to first.

I worked with big, household name, clients and led teams building mission critical components in their technology stack. At the time, I thought being a leader was about being the most knowledgeable and talented person on the team. The person who could answer any technical question that arose…. Then I was given a non-technical leadership position and dropped into a project I knew nothing about. Suddenly I was no longer the smartest person in the room. A quick realization came over me that, without all the answers, I had no idea what true leadership was. My reality was shattered.

Over the next few months, I set out to learn what it meant to be a leader. The more I studied, the more I came to the realization that the number one skill of being a leader is not having all the answers, the number one skill of being a leader is asking the right questions. That led me to coaching. 

You may be thinking, “what does coaching a sports team have to do with this?” It is a fair question. While I am not sure where the term originated, the coaching I am talking about is the practice of using open ended questions and active listening to draw answers out of the group or individual. One of the core tenants in coaching is a belief that you already have the answer within you, it just needs to be discovered. I was hooked on the concept! I even trained and became a certified coach!

I started using coaching techniques with family and friends. I was blown away by how effective it is, and how much easier it made my life, not having to have all the answers. 

When I Implemented coaching with my team I made the #1 mistake that leaders can make while leading a team. 

I turned everything into a coaching session!

My team saw an increase in confidence, efficiency, and output as a result of my new coaching method, but that only went so far. As with everything, coaching is a tool for leaders. It does not replace your knowledge and experience, nor does it replace your guidance and answers. 

Leaders are needed to build and train teams. Leaders must make decisions and provide input. Only asking questions of your team is doing them a disservice. 

I am still 100% sold on coaching as the primary tool all leaders should master, however it is just that- a tool. I encourage you to learn how to coach well, and to also learn how to mentor, make solid decisions, communicate well, and discipline your team members. The more tools you have available the more effective a leader you will be!

Photo by NeONBRAND on Unsplash

How I made my first, CHEAP, aquarium sump

Sump filtration systems are popular amongst fish hobbyist, but in 33 years of fish keeping I have never had one on my tanks. My filter of choice is the good ‘ole under gravel. It can be a hotly debated method, but it has always served me well. Of course I have lived in places with good water straight out of the tap! Currently I am living in a real estate rehab house, and the water quality here is awful for fish. There are wild fluctuations in chemical composition, sometimes the chlorine is overwhelming. I do not trust it, and it has made keeping my tank level a chore.

As much as I like under gravel filters, I recognize their strengths and weaknesses- one weakness being the (slow) speed at which they can process the water. Larger established tanks can absorb spikes ok, but my little 30 gallon struggles under the load…. enter the sump filter.

Sumps can process the entirety of the tank’s water several times per hour, but they tend to be expensive if purchased from a fish store. When you break down their components, it turns out sumps are relatively inexpensive to build yourself.

Here are the major components:

  • Method of getting water out of the tank automatically
  • Filtration method to run the removed water through
  • Method of getting the water back into the tank

That’s it!

“But wait!”, you say, “It cannot be that easy!”

Really, the most difficult part of the project is getting the water out and back into the tank. I promise! The sump itself is simple. It consists of three parts:

  • Mechanical filtration
  • Biological filtration
  • Reservoir of cleaned water

Here is the big picture of my setup:

Getting water out of the tank utilizes a siphon and gravity- no electricity required. Just like the gravel vacuum you use when cleaning your tank. The water return is accomplished via an electric pump in the water reservoir, in this case, at the bottom of a 5 gallon bucket.

A cut away view of my sump looks like:

* The circles with an X in them are the biological filtration.

How does this work?

  • Water comes into the bucket
  • Then it is mechanically filtered via a layer of polyester
  • It tickles through the polyester into several layers of plastic pot scrubbies (yes, that you clean pans with!) that act as the biological filtration media. Bacteria loves to grow on this stuff!
  • Then falls into an empty space at the bottom of the buckets, the reservoir
  • Finally, an electric pump pumps it back into the tank

All told, this system cost less than $60 to build, vs hundreds to purchase from a pet store.

Here is a list of the components I used:

I put a ball valve on the water return line to be able to control the flow rate of water. I believe 250 – 300 gallons per hour is the current flow rate through this system. That means the entire tank water gets processed up to 10 times per hour! That is a lot of filtration….maybe a little overkill. Recommendations I saw around the web were around 5 times per hour as a minimum.

Overall a very simple project. The water clarity is outstanding and the fish are moving around happily. The main caveat here is that since this is a brand new sump it means it has to go through the whole cycling process. I am hoping it will be accelerated though since the tank as it was was cycled already. Some of that good bacteria should make its way into the sump to help prime it.

Have you built a sump before? What is your method?

If this post has inspired you to build one please drop a comment below. I would love to hear about your experience.

A couple great resources on the web for all things fish keeping

World’s cheapest standing desk

Standing desks are great to get you up from a sedentary chair, especially when you are at your desk all day. In my case, a standing desk is helpful to alleviate the pain of a back injury that I suffered in my teens.

I would like a sit/stand convertible desk, however those cost hundreds of dollars and I wanted to be a bit thrifty. This desk cost less than $20 to build. The top is reclaimed from an old shelf, and and I counted it as free.

I also wanted to be able to integrate my musical keyboard into the setup. It came out pretty well for $20!

Page 2 of 162

Powered by WordPress & Beards