Archive for October, 2020

Upgrading LAC to Slim 4

Wednesday, October 28th, 2020 | Programming

Last month, I published an upgrade guide to Slim 4 covering the changes that most people will need to make.

I recently upgraded the Leeds Anxiety Clinic and this project has some further complexities, so this post will elaborate on all of the changes I made, in case my original blog post did not cover some of the issues that you are also running into.

Things to do in advance

The old service layer calls no longer work:

$db = $this->ci->db;

And need to be updated to:

$db = $this->ci->get('db');

However, Slim 3’s service container already supports this syntax, so you can go ahead and update your code in advance.

If you are passing the container into controller functions, you need to typecast it.

use Psr\Container\ContainerInterface;

class Controller {
    __construct(ContainerInterface $ci) {}
}

If you pull the request out from the service container, you cannot do that in Slim 4. For example, if you have a render helper that relies on the request being in the container, you will have to start manually passing that into your render help from the page route closure.

Dependencies

You will want all of these:

"slim/slim": "4.*",
"slim/psr7": "1.*",
"slim/http": "1.*",
"php-di/php-di": "^6.1",

I did wonder if I could remove my other PSR-7 library that I use for Mailgun, nyholm/psr7, but the answer is no.

Instanciating the app

We now create the container and then the app, and you can recursively pass in the container.

$container = new \DI\Container;

$container->set('serviceA', function() use ($container) {
    return new ServiceA($container->get('serviceB'));
});

\Slim\Factory\AppFactory::setContainer($container);
$app = \Slim\Factory\AppFactory::create();

Throwing not found exceptions

If you manually throw not found exceptions or other HTTP exceptions, Slim 3 has them located here:

Slim\Exception\NotFoundException

Slim 4 moves it to here:

Slim\Exception\HttpNotFoundException

Error handling

If you want Slim 4’s default error handling, you need:

$errorMiddleware = $app->addErrorMiddleware(true, true, true);

And if you want to handle HTTP errors (such as not found), you can use:

$errorMiddleware->setErrorHandler(
    Slim\Exception\HttpNotFoundException::class,
    function (Psr\Http\Message\ServerRequestInterface $request) use ($container) {
        $controller = new App\Controller\ExceptionController($container);
        return $controller->notFound($request);
    });

You can also set your own error handling:

$errorMiddleware->setDefaultErrorHandler(
        function (Psr\Http\Message\ServerRequestInterface $request,
        Throwable $exception) use ($container) {
            $controller = new App\Controller\ExceptionController($container);
            return $controller->error($request, $exception);
        });

This means you will need to create your own response object in any exception controller you use.

public function notFound(Request $request)
{
    $response = new \Slim\Psr7\Response;
    return $this->render($response, 'not-found.html');
}

Custom middleware

The way we pass through middleware has changed. The old way:

public function __invoke($request, $response, $next)
{
    $response = $next($request, $response);
    return $response;
}

The new way:

public function __invoke($request, $handler)
{
    return $handler->handle($request);
}

That change alone probably won’t do what you want it to do out-of-the-box, so you might need to do some reading up and adapt it to your specific middleware depending on function.

Custom middleware wants to go towards the bottom after you define your routes, but before the error middleware.

Best winter cycling socks

Tuesday, October 20th, 2020 | Reviews, Video

How do you keep your feet warm during winter cycling? In this video, I will compare regular socks with the Pactimo wool winter socks and Rapha winter socks, as well as discussing what else I do to keep my feet warm when the temperature gets down towards zero.

Mavic Crossmax cycling shoes review

Monday, October 19th, 2020 | Reviews, Video

The Mavic Crossmax SL Pro Thermo shoe is a winter cycling boot. It supports SPD (mountain bike) cleats but Mavic also produces a road version named Ksyrium Pro Thermo that supports SPD-SL cleats.

My hands and feet get very cold on the bike so I have been on a long quest to work out how to keep them warm. These shoes are the closest I have come so far. They do an excellent job of keeping the wind and rain out so when coupled some winter socks your feet should stay toasty.

They have a lot of fastenings. There is a zip along the front and velcro around the ankle. You can tighten it using a boa dial. Once you have the boots on, you can ignore the zip and velcro and just tighten using the dial. My socks do sometimes get caught in the velcro when putting them on.

They are reasonably light. I measured one shoe at 582 grams, which is only 10 grams more than my summer Mavic shoes. That said, my summer Mavic shoes (573 grams) are heavy compared to non-cycling shoes or even my Shimano triathlon bike shoes which come in at around 350 grams each. For a boot, they feel pretty reasonable.

I am a size 11 in a running shoe and I go for an 11.5 when I buy Mavic. That said, they are generously sized, allowing me to get my big socks in them, so I could probably have gone with an 11 as well. They are pricy, so it depends on how much you value warm feet.

Roka Pro Swim pull buoy review

Sunday, October 18th, 2020 | Reviews, Video

A pull buoy is designed to keep your legs floating while doing swim drills so that you can work on improving your arm stroke. In this video, I will review the Roka Pro Swim pull buoy.

What can I say? It’s a pull buoy. It is a good size: maybe slightly larger than the typical pull buoy you get at a pool but not so big that it will not fit in your bag. It has good buoyancy and allowed me to get into my stroke on pull drills. It is symmetrical so you cannot adjust the buoyancy based on its orientation but I never actually do that anyway.

Yoga Sutras of Patanjali

Saturday, October 17th, 2020 | Books

The Yoga Sūtras of Patañjali is a religious text on the practice of yoga. By yoga, I am referring to the full definition of yoga in the Hindu tradition and not merely the asana practice that is popular in the West.

It is made up of 196 verses. I read the longer version that has been translated into English by Sri Swami Satchidananda who also provides extensive commentary on the verses. This was very helpful to understanding. The text is broken down into four books and his commentary on the first two books made them reasonably accessible whereas the second two books, where commentary is limited, were more challenging.

My favourite concept from the book is the idea that anger is a package that you have to accept delivery of. And if you choose not to accept the delivery, the sender is stuck with it. If only it was that simple in real life, of course, but certainly an attitude I would like to cultivate.

LEJOG

Friday, October 16th, 2020 | Family & Parenting, Sport

With COVID forcing everything to go virtual, for this year’s father’s day, we got my dad the Land’s End to John o’ Groats running challenge. It is a 1,744.2 kilometre ultramarathon that winds its way up the UK via a needlessly indirect route and we undertook it as a family.

We set a 140-day (20-week) target. Early days went well because I was polishing off the Great Virtual Race Across Tennessee and even when I stopped running as much everyone else was crushing it. This included a week where my parents walked about 100 miles around Flamborough that pushed us even further ahead.

In the end, we finished in 103 days, 5 weeks ahead of schedule. The biggest contribution came from my dad who filed 542.8 km of the distance himself. We finished it off with a socially-distanced walk using a WhatsApp video call.

Bhagavad Gita

Thursday, October 15th, 2020 | Books

The Bhagavad Gita is a Hindu scripture and is the best-known and most widely read Hindu text. It forms part of the Mahabharata epic and is believed to have been written in the second century BCE.

It tells the story of a dialogue between a warrior named Arjuna and the Hindu god Krishna.

In general, it has a pretty agreeable message. Rejecting monastic life, it calls on us to do our duty in the world while renouncing personal benefit and working towards selfless service. It also offers an attractive afterlife package: life is not one single test that ends in heaven or hell, but a test you can take as many times as you need until you pass. Indeed, even the fear of slipping backwards is removed.

That said, it is easy to see why Christopher Hitchens argues that there is no “answer in the East”. In the first chapter, Arjuna lays down his arms at a great battle. In chapter 2, Kristna tells him that it is his duty as a warrior to fight and that if he does not fight, the other warriors will laugh at him. Nothing like a bit of peer pressure from god to make you go to war. Some have argued the battle is a metaphor for the spiritual battle of good and evil, but this is not widely accepted, especially as the characters in the battle form a major portion of the Mahabharata.

What I like most, though, is that it is short and interesting. Compared to say the Bible, which is really long, or the Qur’an, which is just page after page of repeating that there is definitely only one god and you’re going to be published if you believe anything different.

CBT for Social Anxiety course

Wednesday, October 14th, 2020 | News

My new course, Cognitive-Behavioural Therapy for Social Anxiety, is now live. It is suitable for both individuals looking to access self-help and for counsellors looking to understand the nuances of applying CBT. Preview the course here.

Daffodils sportive

Tuesday, October 13th, 2020 | Sport

Sportives are back! Last week, Bogdan and I completed the Daffodils sportive that starts from Thirsk. It had everyone you would want from Yorkshire: beautiful views, rolling hills, and wide, quiet roads.

It was raining when I woke up and raining the whole way through the sportive. I don’t have any clear glasses so I look a bit miserable in the photos but it is mostly because I was trying to keep the rain out of my eyes: once we were cycling I was enjoying it.

My maximum speed was 57.8 kph coming down one of the hills. Later that day, Filippo Ganna won the prologue (the opening stage time trial) of the Giro d’Italia with an average speed of 58.8 kph.

I hate cold, wet feet so I went full winter gear: winter socks, Mavic winter boots and my Spatz extra-thick overshoes. I did get a little bit of water or sweat in there but they felt warm and dry for the whole event.

Everything took place outside. You had to book your start time down to the minute. This worked better than the long queues at other events although there may have been fewer participants. They had loads of portapotties at the HQ (and I mostly rate events based on their toilet availability) so that was great, and we got a sausage in a bun at the end.

Venla’s 4th birthday

Monday, October 12th, 2020 | Family & Parenting

Last year, we threw Venla a family party that she enjoyed. But what she really wanted was a party with her friends or a “real party” in her words. So, this year, I set a note in my calendar for two months before her birthday to arrange a party with her friends.

Then COVID happened and ended all hopes of that.

Luckily, her birthday fell on a weekday so she could go to daycare and have a party there before coming home to have a party with the three of us.