Tip:
Highlight text to annotate it
X
So I'm jumping back to the file browser and I'm going to open up glue dot routing dot
yml. So here, we're looking at a YAML file. So again we have our key to the left of the
colon, but here we're actually creating an array so instead of a single key value pair,
this actually has several keys and values that belong within it. So this is defined
by a simple indentation.
So in this hello world array, there's a key called pattern, a key called defaults, and
a key called requirements, and you can see that these also consist of arrays which are
indented. So defaults is an array and so is requirements.
One of the benefits of a YAML file is that it's pretty easy to read so you can easily
pick out the bits that are probably important here along with what they mean, so we see
hello world that looks like the URL pattern. We have the slash here to kind of help us
orient ourselves. And that's the pattern, so that makes sense.
We have the permission down here and then permission is access content, and that seems
pretty straightforward. If you're used to working with Hook menu in Drupal 7, this is
the same way that access arguments worked there.
And then finally we have the content of the page which is exactly what our page callback
was in Hook menu. The only difference here is how we're pointing to the function that
actually renders the content. This uses a full namespace to the name of the class, so
Glue Controller, and then points to the specific function that returns this.
So in this way, you can kind of tell that there's a bit of decoupling between this route
and the Controller. You could actually put any function in here that would be available
to Drupal and it would render that as a page. So we're following a particular convention
here.
The hello world title up here is just the name of this route. It is completely arbitrary
as well. It just needs to be descriptive and it needs to be unique. It doesn't have anything
to do with the actual URL or the name of the controller that prepares the content.
One of the powers of this routing configuration system that is hard to see with this simple
example is that you can specify the type of the return value. So by default, what we're
doing is fetching some HTML and displaying it as a page but there will be times where
we would prefer to return JSON for example if we're providing a service through an API
to external sites or we may want to return XML or some other type of data that doesn't
require the entire theming system.
This routing configuration allows us to be far more specific about the types of pages
that we want to return and that helps Drupal know more about what it needs in order to
actually render that data and return it in a way that is expected.