Simple routes problem

Silverstripe Version: 4.1

Battling with a basic route problem.

/news - news list
/news/view/some-post - some-post is a slug per post
/news/tag/some-tag - some-tag is a slug per tag.

The last two work, the first one doesn’t. Error:

[Emergency] Uncaught ArgumentCountError: Too few arguments to function PostController::Tag(), 0 passed and exactly 1 expected

debug_request on /news is

Debug (line 261 of RequestHandler.php): Testing ‘’ with ‘’ on PostController

Debug (line 269 of RequestHandler.php): Rule ‘’ matched to action ‘index’ on PostController. Latest request params: array ( )

My routes.yml file

---
Name: mysiteroutes
After:
   '#rootroutes'
   '#coreroutes'
---
SilverStripe\Control\Director:
   rules:
      news':'PostController'

In PostController:


class PostController extends PageController
{
    private $allowed_actions = ['index','view', 'tag'];

    $url_handlers = [
        '' => 'index',
        'tag/$Permalink' => 'Tag',
        'view/$Permalink' => 'View'
    ];

   public function index(){} 

   public function Tag (HTTPrequest $request){}

   public function View (HTTPrequest $request){}

} 

Why is it even looking at the Tags method?

Changing the routes.yml to 'news': 'PostController'

And the Controller settings to:


private static $allowed_actions = ['index', 'view', 'tag'];

private static $url_handlers = [
	'' => 'index',
	'tag//$Permalink' => 'ViewTag',
	'view//$Permalink' => 'ViewPost'
];

/news works.
new/tag/some-thing - 404
new/view/some-thing - 404

debug_request:

Debug (line 261 of RequestHandler.php): Testing ‘’ with ‘tag/agribusiness’ on PostController

Debug (line 261 of RequestHandler.php): Testing ‘tag//$Permalink’ with ‘tag/agribusiness’ on PostController

Debug (line 269 of RequestHandler.php): Rule ‘tag//$Permalink’ matched to action ‘ViewTag’ on PostController. Latest request params: array ( ‘Permalink’ => ‘agribusiness’, )

Debug (line 261 of RequestHandler.php): Testing ‘$Action//$ID/$OtherID’ with ‘’ on SilverStripe\ErrorPage\ErrorPageController

Debug (line 269 of RequestHandler.php): Rule ‘$Action//$ID/$OtherID’ matched to action ‘handleAction’ on SilverStripe\ErrorPage\ErrorPageController. Latest request params: array ( ‘Action’ => NULL, ‘ID’ => NULL, ‘OtherID’ => NULL, )

Debug (line 185 of RequestHandler.php): Action not set; using default action method name ‘index’

Debug (line 238 of Controller.php): Request handler returned HTTPResponse object to PostController controller;returning it without modification.

Looks like it matches on the third line but doesn’t use it?

This seems to work.

private static $allowed_actions = ['index', 'ViewTag', 'ViewPost'];

private static $url_handlers = [
	'' => 'index',
	'tag//$Permalink' => 'ViewTag',
	'view//$Permalink' => 'ViewPost'
];

public function ViewTag(HTTPRequest $request){}

public function ViewPost(HTTPRequest $request){}

If I change all instances of ViewTag and ViewPost to just Tag and View it doesn’t work.

Maybe it’s a name collision or something. “Tag” is the name of the DataObject. IDK…

1 Like