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

Custom controller (Dataobject as Page)


Go to End


4 Posts   2708 Views

Avatar
FullWebService

Community Member, 38 Posts

17 July 2014 at 8:04am

Edited: 17/07/2014 8:04am

Before 3.1.5 I used to create a routes.yml with:

Director:
  rules:
    'custom//$Action/$ID/$Name': 'Custom_Controller'

And then a Custom_Controller.php file with:

class Custom_Controller extends Page_Controller {

In the custom controller I would make public function with code like:

$data = $data = array(
	"Title" => "My title",
	"Content" => "My content
);
return $this->customise($data)->renderWith(array('CustomTemplate', 'Page'));

Because I extended the Page_Controller I would still have the Sitetree navigation in the page.

As with 3.1.5 this shows me a login screen with the message:

Log in
You must log in with your CMS password in order to view the draft or archived content. <a href="/Custom_Controller/?stage=Live">Click here to go back to the published site.</a>

I actually see the link typed out like that

If I extend just je Controller it works fine:

class Custom_Controller extends Controller {

But now I don't have access to the stuff in my Page_Controller. I have to add all the requirements in the init() of the controller and I have no navigation anymore.

Before 3.1.5 the Page_Controller method worked perfectly! What changed and what is the best practice for this sort of thing?

Thanks in advance.

Avatar
camfindlay

Forum Moderator, 267 Posts

21 July 2014 at 2:46pm

I haven't been able to find anything specific that changed in 3.1.5, only thing I can think of in the controller might be the $allowed_actions static which allows the public function in the controller to be accessed as an action via http.

Avatar
FullWebService

Community Member, 38 Posts

22 July 2014 at 12:20am

Edited: 22/07/2014 12:22am

Thanks for the reply.

I checked and double checked the $allowed_actions again and copied the array from my Custom_Controller to the Page_Controller.

I now have the same $allowed_actions array in Page.php and Custom_Controller.php. This seems to work! I can extend the Page_Controller again. Seems a little odd and it wasn't like this before 3.1.5.

I'm not sure if this is a bug or a feature.

Putting the allowed actions just in Page.php doesn't work, btw. It only works when the array is in both files.

Avatar
webdiv

Community Member, 2 Posts

22 December 2015 at 7:52am

In case anyone else runs into an issue with this type of error, depending on what you're trying to do, this may get your custom controller working:

Versioned::set_reading_mode('Live');

Also, see this bug: https://github.com/silverstripe/silverstripe-framework/issues/3131