Drupal 8 is a Great Way of Developing a Useful and Valuable Website Part Nine


Sharing is caring!

Developing websites can be an enjoyable experience, or it can also be a daunting task, which can make your life miserable; if you don’t know what you’re doing. We are going to talk a bit more about twig in Drupal 8 in this article, then we’ll move on to other Drupal components, which are very important to know.

XSRF attacks can happen to websites, but twig employs auto-escaping and sanitizes all HTML. XSRF or cross-site request forgery (csrf) attack happens when a predator tricks a website user into clicking a link, which forces the browser to execute a command on an application, which was not wanted by the user.

Twig syntax is simplified, and it makes the code cleaners, because the print and render keywords, which can be very confusing, have been eliminated.

Alright now let’s focus our conversation on Routes, which is the routing system that Drupal 8 uses to figure out which requested routes, get a response from which controller. The routes which have been registered in active mode, are used by the Drupal routing system, for Drupal to match incoming request’s path to a controller.

Any custom code in Drupal lives in a module, which is in the module directory, this is the directory used, to create custom routes and controllers. The info file in a module goes into the YAML file, which uses the YAML format, for simplicity. The page that a developer would use for them, to test their module resides at the link that the router explicitly defines

Creating a page in Drupal is a two-step process. First users should create the URL of the page using something called a route. The second step involves creating something called the controller, which is the file that is used to control the page.

In the same directory where the info file was created, the route file is also created, the route file also follows the YAML format for simplicity. In the file the first property or line of code would look like the code below.


  path: /test/to/yoursite  


    _controller: Drupal\(module_name)\Controller\(NAMESPACED Class::METHOD)


    _permissions: ‘access content’

  Alright so the lines of code we see in the example of a route file, tells the Drupal system that my file is an actual controller, the second line of code tells Drupal the path to my newly create module. On the third and fourth line, I am telling Drupal to use the function located at _controller and finally, the last two lines are setting the permission for the page, so that I can access the page. The last line will change depending on your permissions configuration.  

Alright, the route for the page has been created, now it is time to create the controller, which would be used to control the page. The controller goes in the src directory, which would be in the module directory, that was created, inside the src directory a controller directory is also created.

Inside the controller directory, a file called (NAMESPACED.php) would be created there, this is the controller file. This file would contain a class called class NAMESPACED {} which would contain objects that would do something for an application.

A couple of things to remember here, every controller file has something called a namespace Drupal\module_name\Controller. The namespace has a line code, which contains the directory of the path of the controller file and it must start with Drupal, another thing to remember is that the name of the class, must match the name of the controller file. If any of these steps are not followed carefully, then Drupal will not be able to find the class for your module.

Thank you for reading this article!!!