# Use Laravel's route(), url(), and asset() Helpers Inside Markdown

**Author:** Mozex | **Published:** 2026-04-06 | **Tags:** Laravel, PHP, Open Source, Markdown | **URL:** https://mozex.dev/blog/11-use-laravels-route-url-and-asset-helpers-inside-markdown

---


I had a Laravel project designed to be white-labeled. Swap an env file, change the domain, and the whole application launches as a completely different product.

The problem was the Markdown files.

<!--more-->

This project had a lot of Markdown content for pages. Every internal link in those files was a ticking time bomb. I had two choices: relative URLs everywhere (fragile, breaks the moment your route structure changes) or hardcoded full URLs (impossible when the same app runs under different domains).

But hardcoded URLs in Markdown bother me even outside multi-tenant setups. You rename a route six months from now, and suddenly you're grep-ing through every `.md` file praying you caught them all. I've been burned by this enough times to want a real solution.

So I built [CommonMark Routes](https://github.com/mozex/commonmark-routes), a [league/commonmark](https://commonmark.thephpleague.com/) extension that lets you use Laravel's `route()` helper directly inside Markdown. Named routes resolve at render time, so your content stays decoupled from your URL structure. Change a route, rename a path, switch domains: your Markdown doesn't care.

The package has been running in my own production apps since mid-2024. I just released v1.6.0 with `url()` and `asset()` helper support, plus image source resolution for all three helpers.

## Installation and Setup

```bash
composer require mozex/commonmark-routes
```

Register the extension with your CommonMark converter:

```php
use League\CommonMark\CommonMarkConverter;
use Mozex\CommonMarkRoutes\RoutesExtension;

$converter = new CommonMarkConverter();
$converter->getEnvironment()->addExtension(new RoutesExtension());
```

If you use [spatie/laravel-markdown](https://github.com/spatie/laravel-markdown), add it in `config/markdown.php`:

```php
'extensions' => [
    Mozex\CommonMarkRoutes\RoutesExtension::class,
],
```

That's it. Your Markdown can now use Laravel's URL helpers.

## Using route() in Markdown

This was the original feature and the whole reason I built the package. Instead of hardcoding URLs, you reference named routes:

```markdown
[View your profile](route('profile'))
```

This renders as a link pointing to whatever URL your `profile` route resolves to. Route parameters work exactly like you'd expect:

```markdown
[View Post](route('posts.show', ['post' => 1]))
```

Query strings and relative URLs are supported too:

```markdown
[Search Results](route('search', ['query' => 'laravel'], absolute: false))
```

The `absolute: false` named argument generates a relative URL, same as Laravel's native helper.

One neat trick: if you use the helper as both the link text and the URL, the resolved URL appears in both places:

```markdown
[route('homepage')](route('homepage'))
```

This renders something like `<a href="https://example.com">https://example.com</a>`. Handy for reference pages or documentation where you want the actual URL visible.

## New in v1.6.0: url() and asset()

The `route()` helper covers named routes, but not every URL in your Markdown points to one. Sometimes you need a plain URL path or a static file reference.

### The url() Helper

```markdown
[Terms of Service](url('/terms'))
[API Documentation](url('/docs/api'))
```

This generates fully qualified URLs using Laravel's `url()` helper. It respects your `APP_URL` configuration, so the same Markdown produces correct links regardless of which domain serves the application.

### The asset() Helper

This is the one I'm most excited about.

```markdown
[Download Report](asset('files/report.pdf'))
```

If you've deployed anything on [Laravel Vapor](https://vapor.laravel.com/), you know the issue. Your static files don't live on the same server as your application. Vapor serves assets from S3 through CloudFront, which means a simple `/images/logo.png` path in your Markdown won't work.

The `asset()` helper generates the correct URL whether you're running locally, on a traditional server, or on Vapor. Your Markdown doesn't need to know or care about the deployment target.

## Image Source Resolution

All three helpers now work inside image tags too. This was the other half of v1.6.0.

```markdown
![Company Logo](asset('images/logo.png'))
![User Avatar](url('/avatars/default.png'))
![Hero Image](route('images.show', ['image' => 'hero']))
```

The `asset()` helper is the star here. If your Markdown content includes images and you deploy on Vapor, those image paths need to point to your CDN. With this extension, they do automatically.

Regular Markdown images without helpers pass through untouched. Nothing breaks for existing content.

## A Note on Trust

The extension evaluates helper calls from your Markdown source. Use it only with trusted content. If your Markdown comes from your own codebase, your CMS, or a database where only trusted users write, you're fine. Don't process arbitrary user-submitted Markdown through this extension.

## When This Package Saves You

**Multi-tenant and white-label apps.** Your Markdown works across every instance. No domain-specific URLs to maintain per tenant.

**Route refactoring.** Rename a route, change a URL pattern, reorganize your entire routing structure. Markdown links update automatically because they reference route names, not hardcoded paths.

**Vapor deployments.** Asset URLs resolve correctly whether you're local or on Vapor's S3/CloudFront infrastructure. No environment checks in your content.

**Database-stored Markdown.** If you store Markdown in your database and render it on the fly, the helpers resolve at render time. The stored content stays portable and environment-agnostic.

It doesn't matter whether you write Markdown in static files committed to version control or store it in your database. The package handles both.

## Get Started

CommonMark Routes is MIT-licensed and supports PHP 8.2+ with Laravel 11 through 13.

```bash
composer require mozex/commonmark-routes
```

The [GitHub repo](https://github.com/mozex/commonmark-routes) has the full documentation, and the [v1.6.0 release notes](https://github.com/mozex/commonmark-routes/releases/tag/1.6.0) cover everything that's new.