Overriding/Extending the dispatcher.php

Permalink
Hi all,

I am trying to extend the dispatcher.php and I have a couple of problems:

I have added the following code in the dispatcher, right after the part where the dispatcher sets $c to the page that it needs to render:
// Check if we need to re-direct anywhere
      $redirect_url = $c->getCollectionAttributeValue('redirect_url');
      if ($redirect_url != '') {
         // figure out where we need to go
         $c = Page::getByPath($redirect_url, false);
         if ($c->isError()) {
            // if we've gotten an error getting information about this particular collection
            // than we load up the Content class, and get prepared to fire away
            switch($c->getError()) {
               case COLLECTION_NOT_FOUND:
                  $v = View::getInstance();
                  $v->render('/page_not_found');
                  break;
            }
         }

This checks for a page attribute called 'redirect_url', which, if set, it contains a URL to redirect to.

Problem #1: Is this the right place to place this code? What is the most appropriate way to extend/override the dispatcher?

Problem #2: Although I expected it to work, it doesn't! The reason seems to be that not all Attributes are available at this stage.

Any ideas/suggestions welcome!

Thanks,
-Costa

cnrx
 
ryan replied on at Permalink Reply
ryan
I think it would be much better to use a page controller's on_before_render function. The dispatcher isn't intended to ne overridden.
cnrx replied on at Permalink Reply
cnrx
Hi Ryan and thank you very much for your post.

I see your point, but in the on_before_render function I will need to repeat the same things the dispatcher does, and recursively too. In fact, I could use the ability to invoke the dispather again (is that possible?).

How do I implement a page controller? I have no clue how to do this. Could you please give me some info?

I nearly got what I wanted, by creating a Dispatcher model with a couple of methods and then calling those methods from within the dispatcher.php. The Dispatcher model methods call themselves and take care of recursion.

Because the dispatcher is such a central and important part of C5, I think perhaps it should be re-designed as a class so that it allows for extension. This could handle very complex navigation (such as internal-redirects, aliases with children etc.)

Cheers,
-Costa
cnrx replied on at Permalink Reply
cnrx
I brought it down to just one method.

Basically, I moved some code from the dispatcher.php (from '## Get a permissions object for this particular collection.' to just before the '/startup/process.php' call) into a static method inside the Dispatcher class.

It looks pretty neat and it works.

I will now try the on_before_render function and see if that makes any more sense.