Semicolon
Semicolon

Reputation: 1914

SilverStripe setting controller $url_handler through init()

Is it possible to set the (ContentController's) $url_handlers through the init() function as below?

public function init() {
    parent::init();

    $this::$url_handlers = array(
        '' => 'index',
        '$Project' => 'getProject'
    );
}

I'm asking because instead of getting the function called, I'm getting a 404, whereas when 'hardcoding' the $url_handlers in the conventional way private static $url_handlers = ... the code works fine and the function is called.

Upvotes: 1

Views: 99

Answers (1)

scrowler
scrowler

Reputation: 24406

The $url_handlers property is actually a configuration property in SilverStripe terminology. This means that when you flush your cache manifest, the configuration is rebuilt and cached.

You can update it from init, but you need to do it using the configuration API, because by the time your init method is called the configuration manifest has already been parsed. For this reason, modifying the self::$url_handlers property will not have any effect.

Here's an example:

public function init()
{
    parent::init();

    Config::inst()->update(
        __CLASS__,
        'url_handlers', 
        array(
            '' => 'index',
            '$Project' => 'getProject'
        )
    );
}

For reference, here's the point where RequestHandler::findAction looks at the configuration manifest for the defined $url_handlers values.

Upvotes: 4

Related Questions