In this episode, we dive into building out the entire redirect flow for our URL shortener app. We kick things off by grabbing a shortened URL from the database and discussing what needs to happen when a user lands on it — namely: find the record, log the visit, and redirect to the final destination. Plus, we'll make sure that the original URL can be shown in our form when needed.
We get hands-on with Laravel Folio, showing how to create dynamic route pages simply by naming your .blade.php
files appropriately (using square brackets with the model/column you want to bind by). We explore how route model binding works in Folio and how to access the bound URL inside our template, storing it in state if needed.
Next, we wire up the redirect logic itself. You'll see how we use Folio's mount
function to handle the redirect and logging as soon as the component is loaded—no unnecessary output shown to the user. We handle the actual redirect with a 301 status, and increment the visits counter every time a redirect happens.
After that, we improve our form to output the full redirect URL by building it the Laravel way: using named routes. Since Folio doesn't create named routes by default, we show how to easily name your Folio routes using the name()
helper.
Finally, we tidy up by demonstrating which pieces of state are necessary, and which can be removed to keep things clean. By the end of this episode, you'll have a fully functioning redirect route, with route model binding, visit logging, user redirection, and nicely named routes for building links in your app.