Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Framework: Setting the Root Controller Route on 3.1


Go to End


15 Posts   4979 Views

Avatar
Mo

Community Member, 541 Posts

15 March 2017 at 3:44am

Hi Neil,

Have you tried the below?

---
Name: mysiteroutes
After: framework/routes#coreroutes
---
Director:
  rules:
    '$Action/$ID': 'MainJobsPage_Controller'

Also, is this a framework only install or are you using the CMS? I only ask because if you are using the CMS you would be better off making a HomePage page type with associated controller and pulling in your jobs from there.

Cheers,

Mo

Avatar
neilcreagh

Community Member, 136 Posts

15 March 2017 at 4:16am

Hi Mo, thanks for answering so quickly!
If I use that all my website pages go blank - it's like it can't find the Layout folder anymore to find my templates (the main template surround is there). I don't get any error message and ?debug doesn't tell me anything.

Yes I'm using the CMS and I tried using the HomePage Controller instead — and I have the same problem in that I can get it to work with home/show/ID but not from the root (without /home it doesn't work for me)

Avatar
Mo

Community Member, 541 Posts

15 March 2017 at 6:44am

Hi Neil,

If you are using the CMS you will get issues (as you are telling SilverStripe to use your own Controller instead of it's URL router).

Is it safe to assume that you have a Job object (that extends DataObject) and you want to show something like "Latest Jobs" on the Home Page, then when you click the job you get more info?

If not, what exactly are you trying to achieve?

Many Thanks,

Mo

Avatar
neilcreagh

Community Member, 136 Posts

15 March 2017 at 9:03am

Yes exactly, Job object extends DataObject - potentially hundreds of jobs in different categories so they're managed via ModelAdmin

Yes latest jobs on homepage but the jobs will also be emailed & shared on social media so I was hoping to get a nice clean URL for them — i.e. website.com/job/jobtitle-ID

Avatar
neilcreagh

Community Member, 136 Posts

15 March 2017 at 9:09am

Alternatively, if easier, is there a way to hide the action /show — then I could use the main jobs page. So instead of being /jobs/show/ID it could be /jobs/ID

Avatar
neilcreagh

Community Member, 136 Posts

17 March 2017 at 4:43am

Edited: 17/03/2017 4:48am

UPDATE — this was the solution:

In _config/routes.yml

---
Name: mysiteroutes
After: framework/routes#coreroutes
---
Director:
  rules:
    'job': 'MainJobsPage_Controller'

In /code/MainJobsPage.php

private static $url_handlers = array(
'$ID' => 'show',
);

And also remove /show from the $Link generated for Job dataobjects

Avatar
patricknelson

Community Member, 7 Posts

9 May 2017 at 1:50pm

Edited: 09/05/2017 2:16pm

EDIT: D'oh, I feel silly now. As you can see below, I was assuming this would be passed in via ->getVars() and failed to realize SilverStripe uses it's own custom ->param('name') and ->params() methods. I'm leaving this embarrassing post here in the off chance that someone else ends up making the same mistake. I actually figured it out by diving into SS core code and totally glazed over the documentation. </facepalm>

---

Hmm, I'm having the same issue but this doesn't work for me at all. It seems that simply passing your own custom parameters to the root index is practically impossible if at least not properly documented. It's certainly not working intuitively based on the normal syntax. Basically I just want to do something along these lines:

--
Name: mysite-routes
After: framework/routes#coreroutes
---
Director:
  rules:
    'api/my-endpoint': 'MyAPI_Controller'

There are no DataObject's involved, just a plain route to a controller. In this case, I want to simply have URL's like api/my-endpoint/foo and have foo automatically be the variable I define, e.g. ID in ->getVars(). Unfortunately, the following doesn't work (not even if I point it to another action):

<?php
class MyAPI_Controller extends Controller {

	private static $allowed_actions = [
		'index'
	];

	private static $url_handlers = [
		'$ID' => 'index', // Doesn't work
		'//$ID' => 'index', // Also doesn't work
	];

	public function index(SS_HTTPRequest $request) {
		// ... nothing...
		print_r(request->getVars());
	}

}

What am I doing wrong, or, is this some kind of bug?

Go to Top