LaraBlog Package

Introduction

This is a simple, bare-bones blog package for Laravel. The main advantage is that it is easy to install and it supports localization, with English and French language support out of the box. This package is what I use for my blog on this site and on other sites.


Installation

This package uses Laravel's Auth so you need to have that installed before installing this. You can install that with:

php artisan make:auth

Then install with composer:

composer require escuccim/larablog

Then register the components in /config/app.php:

Add the following to the 'providers' array:

Escuccim\LaraBlog\blogServiceProvider::class,
Laracasts\Flash\FlashServiceProvider::class,
Roumen\Feed\FeedServiceProvider::class,

And add the following to 'aliases' array:

'Form' => Collective\Html\FormFacade::class,
'Feed' => Roumen\Feed\Feed::class,

Run the database migrations to create the blog tables and update the users table:

php artisan migrate

To enable the JavaScript features of the administrative pages you need to add the following to the head of your layouts/app.blade.php view:

<script src="/js/app.js"></script>
@stack('scripts')

Note that the script tag needs to be moved from the bottom of the layout to the header.

By default I use my own middleware to determine if the user had administrator privileges. If you wish to use your own middleware or stick with Laravel's Auth middleware you can do so by updating the config (see below).


Usage

This package contains its own routes, models, controllers and views so should work after installation. To access it just go to /blog. This package also includes an RSS feed which is generated using roumen/feed and which is available at /feed.

If you wish to edit my views or other files you can publish them to your application:

php artisan vendor:publish

This will publish all of the files. To only publish certain files add --tag=[group], using the groups listed below:

  • config - publishes the config file to /config/blog.php
  • views - publishes the views to /resources/views/vendor/escuccim
  • lang - publishes the language files to /resources/lang/vendor/larablog

To add additional languages, publish the lang files and then create new directories under /resources/lang/vendor/larablog corresponding to the language you wish to add. Copy the blog.php file into your new directory and put the translations into the new language into the array.

Note that out of the box the blog will use the languages specified in config/app.php under 'locale'. If you wish to have the blog translate on a per request basis you will need to provide the code for this, or use my package escuccim/translate.

If the use_rich_card value in the config is set to true a structured data script will be included in the display of the article. If this is set to true then a field will be shown when adding or editing an article to allow you to specify an URI for an image which will be included in the rich card. The rich card will also look for a logo in /public/images/logo.png which will be displayed if it exists.


Configuration

To access the configuration files you need to publish the config file to /config/blog.php with:

php artisan vendor:publish --tag=config.

The config file has the following values:

  • paginator_length - the number of results to display per page. Defaults to 5.
  • blog_feed_title - the title to be used in the RSS feed
  • blog_feed_description - the description to be used in the RSS feed
  • cache - allows you to use caching for display of blog archives menu so it doesn't hit the database on every request. This uses Laravel's Cache facade which defaults to 'file' and can be changed in the .env files. I personally use Redis as my cache.
  • show_flash_messages - determines whether flash messages appear to notify of success on admin operations and posting comments. I use laracasts/flash to display the messages.
  • middleware - the middleware used to determine if the user has permission to access administrative pages. If you wish to use a different middleware you can replace this with the class of or an alias to the middleware you wish to use.
  • is_user_admin - function used to determine whether to display administrative buttons like add, edit and delete. If you are not using my authorization scheme replace this with the name of a function which returns true if the user is an administrator and false otherwise.
  • use_rich_card - determines whether or not to include a rich card in the display of the article.
  • logo - if you are using rich cards you can specify a URI for an image to be displayed as the logo.
  • include_amp_href - if you have an AMP version of the blog pages this will include a meta tag pointing to the AMP version
  • download_images - if you are using rich cards you can specify an image to associate with the structured data. Setting this to true will download the image to the local server. If it is set to false the image will be loaded from a remote URL if one is specified.
  • image_directory - use this to specify the local directory to download images to. It defaults to accessing storage via a symlink in public/storage.

Note that in order to leave a comment the user must be logged in, so I would not recommend using 'auth' as the middleware as this will allow any registered user to add, edit and delete articles. Also note that the code for displaying comments will reference a field in the user table called 'image' to display an avatar for the user if there is data in the field. If you wish to allow users to set avatars you will need to write the code for this yourself.