2
votes

I'd like to add a default route to Silverstripe. At present, there are some routes in my application, being /dev, /admin and /api. I would like all other trafic that's not in a (virtual) subdirectory to be handled by a separate controller.

I've been playing around with the YAML config system, but I don't seem to be able to wrap my head around it.

So basicly I want to do the following

  • /dev -> basic SS functionality, should be doing what it does :-)
  • /admin -> cms module
  • /api -> Restful api module,

But every other path should point to MyOwnController.

  • /asdfsdfsd -> MyOwnController
  • /23-asdf -> MyOwnController etc.

It's not necessary that /asda/asd also reroutes to MyOwnController.

I made a /mysite/_config/routes.yml file, containing the following:

---
Name: myroutes
After: 'framework/routes#coreroutes'
---
Director:
  rules:
    '$Path': 'MyOwnController'

This breaks the site (only 404's). How should this be set up? I could start fiddling in the _config.php, but I'd rather adhere to the standard :-)

2
You don't need a $ in front of the path - so 'somepath': 'MyController' should work. Also, did you run a /dev/build?flush=1 after making changes? It's a common clanger! - TheHacksaw
"somepath: MyController" would, afaik, only route all traffic of www.mysite.com/somepath to the controller. I want "somepath" to be arbitrary. Like twitter.com/username, where "username" can be anything you want. And yes, I am flushing myself senseless. - jberculo

2 Answers

1
votes

What about doing it via .hataccess? Something like the following (not tested):

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/admin/
RewriteCond %{REQUEST_URI} !^/dev/
RewriteCond %{REQUEST_URI} !^/api/
RewriteCond %{REQUEST_URI} !^/your/default/route
RewriteCond %{REQUEST_URI} !^/framework
RewriteCond %{REQUEST_URI} !^/assets
RewriteCond %{REQUEST_URI} !^/Security
RewriteCond %{REQUEST_URI} !^/themes
RewriteCond %{REQUEST_URI} !^/cms
RewriteRule (.*) http://www.yourdomain.com/your/default/route/$1 [L]
0
votes

I've adapted this from the CMS ModelAsController routes and should work, but is untested.

---
Name: myroutes
Before: '*'
After: '#rootroutes'
---
Director:
  rules:
    '$Path//$Action/$ID/$OtherID': 'MyOwnController'

https://github.com/silverstripe/silverstripe-cms/blob/3.1/_config%2Froutes.yml

Remember to flush=1.