2

I’m trying to use a plugin to set up a site route that will direct every page request to a special template.

This is what I’ve set up in my plugin file.

public function registerSiteRoutes()
    {
        return array(
            '*' => 'special/index',
        );
    }

Using the above doesn’t seem to do anything. If I replace * with a word, then try to visit that URL I get something different but not my template. would love it if someone could put me right here

UPDATE If I change tact a bit and route to a controller using a specific url then I can see it gets there

public function registerSiteRoutes()
    {
        return array(
            'beer' => array('action'=>'beer/index'),
        );
    }

But I can't figure out how to catch all requests using * doesn't appear to do anything

UPDATE @robin helped me out massively here with some more info outside the comments.

Basically in Craft 2 the plugin routes get called first no matter what. So I can hijack the init method to load my special template

UPDATE 2 I made this into a Craft 2 plugin. https://github.com/moresoda/craft-emergency-mode

Craft 3 plugin coming soon.

1

You can use the catch all route

'catchAll' => ['site/offline'],

For yii1 you can also do

'<url:(.*)>' => 'controller/action'
  • Neither of those is having any effect. :( – foamcow Nov 9 '18 at 14:28
  • Do you want to route everything or only the routes it doesn't find? – Robin Schambach Nov 9 '18 at 14:30
  • Everything. I need it to work similar to the offline system. But be able to activate it separately. Maybe it could just 302 every request to another template? – foamcow Nov 9 '18 at 14:41
  • If I remember correctly in Craft 2 you can't control the default entry routes in your plugins so they'll be routed first no matter what. Thus you can just handle the request in your plugins init function and send the response there – Robin Schambach Nov 9 '18 at 14:44
  • That looks like it might do the trick – foamcow Nov 9 '18 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.