Search code examples
url-routingsilverstripesilverstripe-4

How to remove routes added by other vendormodules in SilverStripe 4?


I have installed the Blog module for SilverStripe 4, but do not want all the different routes that it has available.

I want to remove for instance the "profile", "archive" and "tag" routes. Those routes are defined by the module's BlogController class.

How can I ensure these are replaced with a HTTP 404 response?


Solution

  • Within your_module_folder/_config/config.yml_if you indicate it should be processed After the blog module and you define those routes it should overwrite them:

    ---
    name: your_module
    After:
      - 'blog/*'
    ---
    SilverStripe\Control\Director:
      rules:
        'profile/': 'MyCustomController'
        'archive/': 'MyCustomController'
        'tag/': 'MyCustomController'
    

    Please review the routing documentation

    The controller should only have one action that throws a 404 http error.

    use SilverStripe\Control\Director;
    use SilverStripe\View\Requirements;
    
    class MyCustomController extends Controller {
    
        private static $allowed_actions = ['index'];
    
        public function index(HTTPRequest $request) {
            return $this->httpError(404, "Not Found");
        }
    }