Reader small image

You're reading from  Drupal 10 Development Cookbook - Third Edition

Product typeBook
Published inFeb 2023
PublisherPackt
ISBN-139781803234960
Edition3rd Edition
Tools
Concepts
Right arrow
Authors (2):
Matt Glaman
Matt Glaman
author image
Matt Glaman

Matt Glaman is an open source developer who has been working with Drupal since 2013. Since then, he has contributed to Drupal core and over 60 community projects. He is also a speaker at multiple Drupal community events, including DrupalCon. Matt is currently a principal software engineer at Acquia and was previously a product lead at Centarro, helping maintain Drupal Commerce.
Read more about Matt Glaman

Kevin Quillen
Kevin Quillen
author image
Kevin Quillen

Kevin Quillen has been working with Drupal since 2006. He's contributed several community modules, and built websites using Drupal for brands such as Dogfish Head Craft Brewery, the National Bureau of Economics, Harvard University, MTV, Yale University, Verizon, the Museum of Science, and countless others. You can find him engaged and helping community members on Slack, Drupal StackExchange, or sharing his thoughts on his personal blog. Kevin is also an Acquia Triple Certified Drupal Expert, an Acquia Certified Site Studio Site Builder, and an Acquia Certified Cloud Platform Pro. He is currently a principal developer and Drupal practice lead at Velir.
Read more about Kevin Quillen

View More author details
Right arrow

Creating a dynamic redirect page

Controllers for routes in Drupal can return request objects provided by the Symfony HttpFoundation component instead of a render array. When a response object is returned, the render system is bypassed and the response object is handled directly.

In this recipe, we will create a route that redirects authenticated users to the homepage and anonymous users to the user login form.

How to do it…

  1. First, we need to create the src/Controller directory in the module’s directory. We will put our controller class in this directory, which gives our controller class the Controller namespace:
    mkdir -p src/Controller
  2. Create a file named RedirectController.php in the Controller directory. This will hold our RedirectController controller class.
  3. Our RedirectController class will extend the ControllerBase base class provided by Drupal core:
    <?php
    namespace Drupal\mymodule\Controller;
    use Drupal\Core\Controller\ControllerBase;
    class RedirectController...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Drupal 10 Development Cookbook - Third Edition
Published in: Feb 2023Publisher: PacktISBN-13: 9781803234960

Authors (2)

author image
Matt Glaman

Matt Glaman is an open source developer who has been working with Drupal since 2013. Since then, he has contributed to Drupal core and over 60 community projects. He is also a speaker at multiple Drupal community events, including DrupalCon. Matt is currently a principal software engineer at Acquia and was previously a product lead at Centarro, helping maintain Drupal Commerce.
Read more about Matt Glaman

author image
Kevin Quillen

Kevin Quillen has been working with Drupal since 2006. He's contributed several community modules, and built websites using Drupal for brands such as Dogfish Head Craft Brewery, the National Bureau of Economics, Harvard University, MTV, Yale University, Verizon, the Museum of Science, and countless others. You can find him engaged and helping community members on Slack, Drupal StackExchange, or sharing his thoughts on his personal blog. Kevin is also an Acquia Triple Certified Drupal Expert, an Acquia Certified Site Studio Site Builder, and an Acquia Certified Cloud Platform Pro. He is currently a principal developer and Drupal practice lead at Velir.
Read more about Kevin Quillen