New package

Introducing Laravel Pages

Samuel Štancl

Samuel Štancl

Founder

Published on

I'm excited to announce the release of Laravel Pages, a package that lets you add routes to your Laravel app by creating Markdown files or Blade views.

Essentially, you create either content/pages/foo.md or resources/views/pages/foo.blade.php and the page will be accessible on the /foo route.

Markdown files use a pre-defined Blade view to get rendered. Blade files are meant for pages which don't follow the default layout and need more custom styling.

Example use cases

For instance, you could have the /pricing route use a Blade file (pages/pricing.blade.php) with a pretty design that accompanies your pricing copy.

Whereas for /about, you could have a simple Markdown file (content/pages/about.md) that describes your service using pure text without any special graphical elements.

We use this on our website — the About, Careers, and Open Source pages are simple Markdown files.

How it's used

The package uses Ryan Chandler's great Orbit package for the Markdown files.

Pages stored in content/pages/*.md are actually available as Page models. This means that the pages can contain metadata such as the page title.

That metadata is simply stored as YAML front matter:

slug: about
title: 'About us'
updated_at: 2021-05-19T19:09:02+00:00
created_at: 2021-05-19T19:09:02+00:00

Those are markdown pages.

Sometimes, however, you may want a page to use a custom template.

For example, as in the use case mentioned above — the pricing page may need a better design than a simple block of Markdown.

To do that, simply create a view named pages/pricing.blade.php and it will be immediately available on /pricing.

SEO integration

The package also automatically integrates with Laravel SEO — another package of ours.

Laravel Pages sets the meta title to the title property of the page, and the meta description to an excerpt of the page content.

All you need to do is add the following line to your layout file:

<x-seo::meta />

Password-protected routes

The package also lets you protect certain routes with passwords. To add a password to a page, simply specify the password key in the YAML front matter:

password: 'foo'

Now if a user wants to visit the page, he will have to include ?password=foo in the URL, otherwise he'll be presented with a 403 error.

- /about
+ /about?password=foo

Easy to test

The package is, of course, fully testable. You can write tests exactly the same way as you would test content pages implemented by you manually. Here's an example test from the package:

test('pages can be password-protected', function () {
    Page::create([
        'slug' => 'test',
        'title' => 'Test page',
        'password' => 'foo',
        'content' => 'This is a test page'
    ]);

    please()->get('/test')->assertForbidden();
    please()->get('/test?password=bar')->assertForbidden();
    please()->get('/test?password=foo')->assertSuccessful();
});

(The test is written using Pest and a custom helper to make the code pretty & support VSCode intellisense.)

And that's it for this article. If you find the package valuable, we’d appreciate if you gave it a try and starred it on GitHub. We also welcome feature suggestions!

You can find the GitHub repository here: archtechx/laravel-pages.

Lean Admin is launching this year!