How to store a JWT in a cookie and auto-refresh the token using LexikJWTAuthenticationBundle

Looking to store your JWT in a cookie and auto refresh it before it expires when using the LexikJWTAuthenticationBundle? There’s a setting that tells the bundle to look for a JWT cookie, but there’s nothing that actually creates the cookie for you. That needs to be done manually. I’m going to assume if you’re here, you already have the bundle installed and are just looking for a way to store the JWT in a cookie, so I’m going to get straight to how you do it. Update the LexikJWTAuthenticationBundle config To tell the bundle that you want it to look

Continue reading

Finishing the Question/Answer App

Creating a website with Symonfy 4: Part 4 This post is part of a series where we’ve been building a Question/Answer web app. So far we have our login system built and the ability to ask a question. Today we’ll be updating our homepage to pull questions from the database, we’ll add the ability to answer questions, update our menu and add a way to track views on a question. If you haven’t been following from the start, you can download the code up to this point here. With that said, let’s get started! Updating our Menu To get the

Continue reading

Working with Symfony 4 Forms & Doctrine Entities

Creating a website with Symonfy 4: Part 3 In today’s post we’ll be adding the ability to ask questions! If you haven’t been following along, this is a tutorial series so if you want to start from the beginning check out this post first. If you want to jump ahead, click here, to download a copy of the code. In this part we’re going to be adding some menu links and showing different ones depending on if the user is logged in, we’ll create the form to ask a question and then a page that displays the question. Let’s get

Continue reading

Symfony 4 Login & Registration System

Creating a website with Symonfy 4: Part 2 In the previous post, we went over how to set up our Symfony 4 project, got a little taste of Twig and created our main home page controller. Today, we’re going to build off of last week’s tutorial and create a login and sign up system for our app! If you aren’t following along from the previous post, I highly recommend it if this is your first time working with Symfony 4. But, if you want to skip ahead to this one, feel free to download the starting source code here. Installing

Continue reading

Creating a website with Symfony 4: Part 1

Today we’ll start building a lightweight Question/Answer app in Symfony 4. Kind of like a simplified version of Stack Overflow. In this series we’ll cover Symfony 4 authentication, forms, Twig, Doctrine and much more! This tutorial is mainly meant for people who are either looking to switch to Symfony 4 from another framework or someone whose been learning PHP and is ready to take the next step to using a framework. I’m aiming to make this more a crash course than something extremely in depth, which should hopefully show you how to get up and running with Symfony without dragging

Continue reading

Creating a Web App with Symfony 4: Setting up Routes

This is a continuation from the previous post on Entity Services. If you haven’t read it yet, click here! We’re in the final stretch of our backend API! We have our entities and services created, now let’s work on creating the controllers for our app. Step 1: Creating an Address Endpoint As we did with the BaseEntityService, we can make a base controller that can work with most of our entities. We’ll create a base controller that has methods to do the basic CRUD (create, read, update, delete) operations. Then extend that base controller for each entity controller. Before we

Continue reading

Creating a Web App with Symfony 4: Entity Services

This is a continuation from the previous post. If you haven’t read it yet, click here! Now that we have our entities, we need to make some services that can handle managing them, similar to the UserService we have already. Since these entity services are going to mostly be doing the same thing (creating, validating and saving), we’re going to make a base class that handles that functionality. Then we’ll extend the base class for the individual entity services so there isn’t a ton of code duplication. So let’s get started! In the src/Service/Entity folder, create a new file called

Continue reading

Creating a Web App with Symfony 4: Doctrine Entities

In this tutorial we’re going to start building a resume builder web app. We’ll be building off of the code that we wrote in the previous tutorials. If you haven’t been following along and want to get a head start, you can download the starting code here. Though it’s recommended to start from the beginning if your completely new to Symfony. We’ll be using everything we learned in the previous tutorials to make this web app happen. First we will update our User entity to include some extra details that normally appear on a resume, like address, phone and name,

Continue reading

Working with Services in Symfony 4

In Symfony, Service classes generally are used to hold code that performs repeatable tasks. For example, say you needed to format a phone number. You would make a service class with a method that formats the phone number as opposed to copying and pasting the same bit of code around each time you needed to format a phone number. Services are also a good place to store your business logic code. Code that doesn’t really belong in your controller, entity or repository classes. So far we’ve built out an API which lets a user register, login and see their account

Continue reading

Building a JWT Authenticator in Symfony 4

This tutorial is a continuation of last week’s post on creating a backend API with Symfony. Today we will be implementing authentication with a JWT. JWT stands for JSON Web Token. In practice, a JWT is generally used as a way of storing the user’s session off of the server. That way, your API can stay stateless. If you followed along from the last tutorial, currently after we login, the session is stored on the server. That’s perfectly fine to do, and it’s how a lot of websites store sessions. However, by making your API stateless you remove the extra

Continue reading