WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.036 --> 00:00:04.976 align:middle
Before we go further into customizing
the look and feel of our site,

00:00:05.266 --> 00:00:13.636 align:middle
I want to fix the skill URLs so that instead
of just /mashing, the page is /skills/mashing.

00:00:14.266 --> 00:00:20.706 align:middle
Remember: the fact that our Contentful
content instantly has URLs on our site comes

00:00:20.706 --> 00:00:23.366 align:middle
from the Contentful package
we installed earlier.

00:00:24.176 --> 00:00:28.086 align:middle
But that magic has nothing to do with Layouts.

00:00:28.466 --> 00:00:34.376 align:middle
So, customizing this URL is also
specific to Contentful, not Layouts.

00:00:34.656 --> 00:00:36.756 align:middle
But... I really want to fix it.

00:00:37.486 --> 00:00:42.706 align:middle
Over in the src/Layouts/ directory, create
a new class called ContentfulSlugger.

00:00:42.706 --> 00:00:47.786 align:middle
Make this implement EntrySluggerInterface...

00:00:47.976 --> 00:00:54.376 align:middle
and then generate the one method we need:
getSlug(): We're going to set things up so

00:00:54.376 --> 00:01:01.216 align:middle
that this method is called when the dynamic URLs
for all Contentful entries are being created.

00:01:01.726 --> 00:01:07.536 align:middle
It will allow us to control the "slug",
which is really the URL for each item.

00:01:08.526 --> 00:01:15.606 align:middle
To make life easier, use FilterSlugTrait to get
access to a method we'll use in a minute: Ok,

00:01:16.076 --> 00:01:20.256 align:middle
on Contentful, we have both
Skills and Advertisements.

00:01:20.496 --> 00:01:24.756 align:middle
But we don't really want
advertisements to have their own page.

00:01:25.326 --> 00:01:28.256 align:middle
Unfortunately, with the Contentful integration,

00:01:28.576 --> 00:01:33.796 align:middle
there's no way to disable URLs
for one specific content type.

00:01:33.796 --> 00:01:37.186 align:middle
I'll talk about how to work
around that in a minute.

00:01:37.776 --> 00:01:42.556 align:middle
Anyways, this method will be passed
both skills and advertisements.

00:01:43.016 --> 00:01:50.136 align:middle
Use the new PHP match() function to match
$contentfulEntry-&gt;getContentType()-&gt;getId().

00:01:51.116 --> 00:01:56.766 align:middle
That will return the internal name for each
type, which you can find in Contentful.

00:01:57.386 --> 00:02:02.746 align:middle
If it's skill, return /skills/
then $this-&gt;filtersSlug() -

00:02:03.306 --> 00:02:08.026 align:middle
that comes from the trait - passing
$contentfulEntry-&gt;get('title'):

00:02:09.146 --> 00:02:16.596 align:middle
For advertisement, return /_ad for
all of them: At least, at this point,

00:02:16.766 --> 00:02:19.796 align:middle
only one ad could ever have a page on our site:

00:02:20.316 --> 00:02:24.496 align:middle
if the user went to /_ad, it
would match the first one.

00:02:25.326 --> 00:02:31.516 align:middle
At the bottom, throw a new Exception with
"Invalid Type": So, yes, at this point,

00:02:31.646 --> 00:02:34.556 align:middle
advertisements will still have their own page.

00:02:35.026 --> 00:02:37.986 align:middle
There's no way to turn that off out-of-the-box.

00:02:38.086 --> 00:02:41.746 align:middle
But if you care enough, I
would map all advertisements

00:02:41.746 --> 00:02:45.336 align:middle
to the same URL or URL pattern like this.

00:02:45.676 --> 00:02:52.286 align:middle
Then I would create a route &amp; controller
with the same URL and return a 404.

00:02:52.916 --> 00:02:56.846 align:middle
That route will take precedence
over the dynamic one.

00:02:57.606 --> 00:03:03.266 align:middle
To tell Contentful to use our slugger,
we need to, of course, give it tag!

00:03:03.926 --> 00:03:10.436 align:middle
Add #[AutoconfigureTag] and this one is called
netgen_layouts.contentful.entry_slugger.

00:03:11.416 --> 00:03:13.476 align:middle
This also needs a type option...

00:03:13.636 --> 00:03:15.536 align:middle
which you can set to any string.

00:03:16.076 --> 00:03:20.596 align:middle
Let's use default_slugger: How is that used?

00:03:21.196 --> 00:03:26.566 align:middle
In config/packages/, we need to create a new
config file for the layouts contentful package.

00:03:27.026 --> 00:03:30.146 align:middle
Let's call it netgen_layouts_contentful.yaml.

00:03:31.226 --> 00:03:33.366 align:middle
Repeat that for the root key.

00:03:35.036 --> 00:03:43.506 align:middle
Below, add entry_slug_type, then default set to
the type we used in our tag: default_slugger:

00:03:44.176 --> 00:03:49.146 align:middle
This funny syntax says: For
every content type in Contentful,

00:03:49.486 --> 00:03:53.186 align:middle
use default_slugger when generating the URL.

00:03:53.576 --> 00:03:56.046 align:middle
So, use our ContentfulSlugger.

00:03:57.146 --> 00:03:58.886 align:middle
Ok, done! But...

00:03:59.116 --> 00:04:02.186 align:middle
this is not called when we reload the page.

00:04:02.546 --> 00:04:06.706 align:middle
Nope. This is called when we
"sync" our content from Contentful.

00:04:07.476 --> 00:04:09.226 align:middle
Ok, let's re-sync!

00:04:10.186 --> 00:04:17.216 align:middle
At your terminal, run: symfony console
contentful:sync This updates our local database

00:04:17.216 --> 00:04:19.836 align:middle
with the latest data from Contentful...

00:04:20.076 --> 00:04:22.106 align:middle
and it worked just fine.

00:04:22.516 --> 00:04:28.756 align:middle
But when we run: symfony console
contentful:routes The URLs didn't change!

00:04:29.406 --> 00:04:30.646 align:middle
This is a quirk...

00:04:30.796 --> 00:04:35.266 align:middle
or maybe a feature so that
existing pages don't break.

00:04:35.656 --> 00:04:41.216 align:middle
Either way, once a route is imported
the first time, it's URL never changes.

00:04:41.746 --> 00:04:47.536 align:middle
The easiest way to reset things is to drop
the routes table and reimport everything.

00:04:47.946 --> 00:04:49.556 align:middle
And, this is kind of fun.

00:04:49.846 --> 00:04:56.016 align:middle
We can run: symfony console
doctrine:migrations:migrate current-1

00:04:56.726 --> 00:05:00.096 align:middle
That will reverse the most recent migration,

00:05:00.336 --> 00:05:04.306 align:middle
causing the contentful and
route tables to be dropped.

00:05:05.326 --> 00:05:11.176 align:middle
Put them back with: symfony console
doctrine:migrations:migrate Re-sync the content

00:05:11.176 --> 00:05:15.976 align:middle
again: And now check the routes: Yes!

00:05:16.196 --> 00:05:19.456 align:middle
The URL is /skills/mashing!

00:05:20.046 --> 00:05:25.336 align:middle
So, over on /mashing, we get
a good-old fashioned 404.

00:05:25.816 --> 00:05:28.986 align:middle
But /skills/mashing works.

00:05:29.916 --> 00:05:34.666 align:middle
Next: we don't yet have a page
that lists all of the skills.

00:05:35.186 --> 00:05:35.956 align:middle
Let's fix that!

