This episode is for members only

Sign up to access "Build A Static File Blog with Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
03. Working with Sheets

Transcript

00:00
By the end of this episode, we will have created a couple of blog posts.
00:03
We will be able to dump them out on the index just here, ready to start iterating through and extracting more data from. But the question is, how do we grab these files and actually get this information in the first place?
00:19
Well, there's a really, really good package called Sheet, which will allow us to store and retrieve static content in plain text files. That includes Markdown files, and it does actually include a parser for Markdown as well, which we are going to be tweaking a
00:37
little bit later to change around our syntax highlighting. But we can go ahead and just install this package, create a couple of files, set up a disk and just see these files rolling. So if we head over to the installation
00:51
section for this, let's go ahead and just pull this in as per the instructions. We'll get the config published as well, because we are going to need to modify that as well. And then we can just start to create some files out and dump these out on the page.
01:04
So we're actually going to start over in the config for Sheets here, because this is really important. The first thing that we need to do is set up a collection so we can have multiple collections of things depending on the content that we're pulling in.
01:18
In our case, we just have one because we just have posts within our blog. But if you later on had other things that you needed to pull in, you could go ahead and configure them as well. So let's get rid of the comments wrapping this and let's take a look
01:32
at the things that we need to configure here. The first thing for our posts collection, which we're not going to rename, although you can rename these if you want to, is the disk.
01:42
So this will be the Laravel file system disk that these specific files within this collection are read from. So we're going to need to configure that in just a minute when we choose where we want these to come from, and these can come from anywhere.
01:56
We're going to be putting them on our file system within our application. But these could come from a remote file system somewhere if you wanted to. Now, the second thing here is the sheet class. So every single file that we read in with this package will be bound to a class so
02:13
we can kind of work with it like an Eloquent model. We're obviously not going to be using Eloquent because we're not hitting the database, but we can still create a model to represent each of our objects or posts, which means we can add helper methods onto these and all that kind of stuff.
02:28
The third thing here is the path parser, which will be how these files are read. So we may need to change that later, but we'll see how we go when we start to create these files out and the structure we want these two files to be in. Then we've got a content parser, which we are definitely going to change.
02:45
And we're even going to go ahead and create our own parser a little bit later when we get to syntax highlighting. And then last of all, obviously, self-explanatory will be the extension of these files.
02:56
So already I know that we're going to be working with Mark down here. So I'm going to change this over immediately to MD. OK, so let's start with the sheet class because we don't have a class for this at the moment.
03:08
I'm just going to go ahead and create out a model with Laravel. So I'm going to call this post, of course, not creating a migration or anything like that because we're not dealing with our database. Now, if we head over to that post model,
03:20
we don't want this to extend an eloquent model because we're not hitting the database. What we want this to extend is sheet from that package that we've just created, and let's just re-index things here to pull that in. So we want this to extend the base sheet class just here from this package so we
03:38
can get rid of model now and we can even get rid of has factory because we don't necessarily need this here. So that's pretty much our post model set up ready to go. When we start to read in all of the posts
03:50
that we have within this specific location, they will now be represented as post models. So the last thing to do is to go ahead and figure out where we want these to be pulled from. Now, to do this, we're going to head over to our file systems config within Laravel,
04:06
which contains multiple disks. We've got local, public, S3. So as I said before, you could store these on S3 if you wanted to. But what we're going to do is just keep this really simple and we're going to have
04:17
a local driver, but we're just going to call this posts. And of course, you can tweak this around later depending on where you want to store these. If you want to store them remotely, it's entirely up to you. So I'm going to call this disk posts.
04:29
So that's the disk that we're referencing just here. Now, the driver is going to be local and we're going to put these in our storage path under app, but we're going to put these under a hosts directory. So let's create that directory out.
04:42
So let's find our resources section here. One of our resources, our storage section under app. We're going to create out a host directory. The reason I'm putting in here is this is not public.
04:54
So no one will be able to access the original Markdown files unless they were a public file. So they are private. No one can access them. But of course, this package can still read them.
05:06
OK, now we've done this. What we can have a go at is just dumping out what we've got so far. And of course, we're not going to get any files in here. So let's come over to our post index
05:17
controller and just play around with how we access these. So to access these, rather than using the model directly, we use these sheets facade and then we go ahead and specify the collection that we want to use. We know that's posts.
05:30
We've already defined that. That is what we want. Let's go and just say all. So there's a couple of methods that we can use here.
05:37
We can use get to get an individual one. But we're just going to dump out all of these. So that's die dump on every single one of these. And we will head over to the browser and just give this a refresh.
05:49
OK, so what have we got here? We've got a standard Laravel collection. Nothing is different to if we were working with the database. And once we start to create our files,
05:59
we're going to end up with a collection of posts. So let's start to create our file and just see what we get. So we're going to go over to our posts directory here, and of course, this is where we need to be careful
06:11
about the structure of how we create this out. So the default structure of this will be the date, first of all. So we're going to go ahead and use the year month day format to be really, really careful of that.
06:23
So let's just say what day are we on today? Let's just say the 28th of this month. And then we're going to go ahead and use a dot. And then we're just going to give the slug of the post.
06:34
So let's call this building a blog in Laravel. And of course, we're going to end that with a markdown extension. So now that we've done that, let's head over and just see the difference here. So, OK, at post not pound,
06:47
that's probably a good thing that we've caught that. Now, if we come over to config and sheet, let's make sure we use app models and post in here, give that a refresh. And here we go.
06:58
We've now got one item in this collection, which is a post. We have got the path to this and we have several attributes here as well. So this package has converted the date into a carbon instance for us, which is really handy because now we can
07:14
format this date in any way that we want. We have got a slug, which is great. We can use that for a root model binding a little bit later, which we're going to need to create our own custom root model binding for.
07:25
And we've got the contents of this thing. So at the moment, of course, that is completely empty. Now, what we want to do with this is go ahead and add in a little bit more information for this specific blog post.
07:40
So remember from the introduction, we saw that we had three hyphens here and then we had a bunch of properties like the title of the article. And then down here we had the body of the article. We're going to go ahead and change that up in a bit.
07:54
But let's head over to the next episode and start to list these blog posts out over on the index. First of all, style it up and then we can go ahead and check in how we can introduce this kind of metadata.
13 episodes1 hr 11 mins

Overview

Let's build a ridiculously fast blog from scratch in Laravel, entirely driven by markdown files. No database required!

Your blog will feature everything you're used to, like syntax highlighting, post tagging and pagination. Plus, it'll render in record time.

Once you're done with the course, you'll be able to add on additional features with ease, style it up how you want and quickly create new posts since there's no need for an admin panel.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.