tractorcow / silverstripe-datelink

Allows you to add wordpress style year/month URLs to time-sensitive sections of your website
9 stars 8 forks source link

Refactoring for SilverStripe 4 compatibility #9

Open JorisDebonnet opened 6 years ago

JorisDebonnet commented 6 years ago

Work in progress for #8.

Everything works except actually loading the BlogPost on that URL; it shows "Page not found".

When I print_r(Director::config()->get('rules')) at the end of RegisterRoutes, the last two entries are:

    [$URLSegment//$Action/$ID/$OtherID] => SilverStripe\CMS\Controllers\ModelAsController
    [news/2018/$Month!/$URLSegment!//$Action/$ID/$OtherID] => Array
        (
            [Controller] => TractorCow\DateLink\Routing\DateLinkController
            [ParentID] => 10
            [Year] => 2018
        )

I am on the URL /news/2018/3/unrelated-news-entry/ which shows that debug info on top, but then shows an ErrorPage with "Page not found". I don't understand why it seems to be "ignoring" that it's supposed to be using the DateLinkController, which was successfully loaded into Director's rules config.

How might I debug that?

(after I fix this, I may have one more question about making it compatible with Fluent, since it seems to write only one rule per localized page)

tractorcow commented 6 years ago

The problem here is that the wildcard link is BEFORE the specific news urlsegment. That means the 404 handler will be routed before your news link, hence the symptoms you've experienced.

Instead of modifying director config (harder) you should use the new extension point on Director $this->extend('updateRules', $rules);

With this extension point, you can modify the $rules in place, meaning you can do things that you couldn't do with config, such as inject rules into the middle of the array, or push / unshift new rules on either end.

Making this compatible with fluent is possible but yeah that's a whole another bunch of work. :) Look at fluent's code (extension on Director) to see how it modifies the rules.

JorisDebonnet commented 6 years ago

Thanks for your comments! I tried a quick-and-dirty approach by popping off the wildcard rule, adding the datelink one, and then re-adding the wildcard rule at the end, but this still did not fix the issue.

Guess I'll need to take a different approach anyway to make it compatible with Fluent... so I'll revisit this a bit later with a Director extension.