Blog Detail

02

May
A Drop-in Flat-File Aloia CMS to Store Contents for Laravel cover image

arrow_back A Drop-in Flat-File Aloia CMS to Store Contents for Laravel

Aloia CMS is a drop-in flat-file CMS built on top of Laravel. Aloia CMS is a flat-file CMS that does all the dirty work developers don’t want to do when making a custom content management system. Yet still allowing developers to control every single aspect of presenting the content. The fact that Aloia CMS is a flat-file CMS means that it doesn’t require a database of any kind. All content is saved on the filesystem in a variety of file types (json, txt, HTML, md). Since data is not saved in a database, serving the content is almost instant. People like fast websites, so this content management system won’t get in your way when it comes to performance, like some others.

Installation

You can include this package through Composer using:

composer require roelofjan-elsinga/aloia-cms

and if you want to customize the folder structure, then publish the configuration through:

php artisan vendor:publish --provider="AloiaCms\\AloiaCmsServiceProvider"

Creating a custom content type

Creating a custom content type is very simple. All you have to do is create a class that extends AloiaCms\Models\Model, specify a folderpath</code>,andoptionallyaddrequiredfieldsto<code>folder_path</code>, and optionally add required fields to <code>required_fields. An example can be found below:

namespace App\Models;

use AloiaCms\Models\Model;

class PortfolioItem extends Model
{
    protected $folder = 'portfolio_items';

    protected $required_fields = [
        'name',
        'github_url',
        'description',
    ];
}

Once you have a class like this, you can interact with it as described under “Usage of models”

Built-in models

There are 4 built-in models which you can use without any additional set-up:

  • Page
  • Article
  • ContentBlock
  • MetaTag

Of course, you can add your own models as described in “Creating a custom content type”.

Usage of models

In this example, we’re looking at one of the built-in content types: Article. You can use these same steps for all classes that extend from AloiaCms\Models\Model.

To load all articles in the “articles” folder in the folder you specified in config(‘aloiacms.collection_path’) you can use the following script:

use AloiaCms\Models\Article;

/**@var Article[]*/
$articles = Article::all();

You can use that to display your posts on a page. You can also load a single post, using:

$post_slug = 'this-post-is-amazing';

$article = Article::find($post_slug);

If you only want all published posts, you’ll need to retrieve them like so:

$published_articles = Article::published();

To get the raw contents of each article (content + front matter), you can use:

$post_slug = 'this-post-is-amazing';

$articles = Article::find($post_slug)->rawContent();

And finally, to update your article, you can run:

use Carbon\Carbon;

$post_slug = 'this-post-is-amazing';

Article::find($post_slug)
    ->setExtension('md') // md is the default, but you can use html as well.
    ->setMatter([
        'description' => 'This post is about beautiful things',
        'is_published' => true,
        'is_scheduled' => false,
        // Either use post_date in setMatter() or setPostDate()
        'post_date' => date('Y-m-d')
    ])
    ->setPostDate(Carbon::now())
    ->setBody('# This is the content of an article')
    ->save();

Content blocks

You can manage small content blocks for your website through this package.

The content of the blocks are stored in a folder called “content_blocks” inside of the config(‘aloiacms.collections_path’) folder.

You’ll need to register the facade into your application, by placing the following line to your aliases in config/app.php:

'Block' => \AloiaCms\Facades\BlockFacade::class,

Now you can use the facade in your blade views by using:

{!! Block::get('content-file-name') !!}

For more details, you can visit its source code and documentation on Github.

Published at : 02-05-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project