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.

Data Model Questions /

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

Render page without having it in the database.


Go to End


1160 Views

Avatar
micahsheets

Community Member, 165 Posts

23 September 2014 at 1:50pm

I used to do this in SS 2.4. Where I would create a new page and render it with a controller but never save it to the database. I would like to do this with SS 3.1 however I always get "page not found".
Here is what I am doing and I am wondering what would be the right way to do this?

class Machine_Controller extends ContentController {
	
	private static $allowed_actions = array('view');
	
	public $machine = '';
	
	private static $url_handlers = array(
               '$Slug!' => 'view',
	);
	
	public function init(){
		if (isset($this->urlParams['ID']) && is_numeric($this->urlParams['ID'])) {
			$machine = Machine::get()->byID($this->urlParams['ID']);
			
      if ($machine) {
      	SSViewer::set_theme(SiteConfig::current_site_config()->Theme);
      	$this->machine = $machine;
      	$tmpPage = new Page();
	$tmpPage->Title = $machine->Title;
	$tmpPage->URLSegment = $machine->URLSegment;
        $tmpPage->MetaTitle = $machine->MetaTitle;
        $tmpPage->MetaDescription = $machine->MetaDescription;
        $tmpPage->MetaKeywords = $machine->MetaKeywords;
        $tmpPage->ExtraMeta = $machine->ExtraMeta;
	$tmpPage->ID = -1 * rand(1,10000000);
	$tmpPage->Content = 'Test machine content';
	$tmpPage->CanViewType = "Anyone";
	$this->dataRecord = $tmpPage;
      }
		}
		parent::init();
		
	}
	
	public function view() {
		$data = array(
			'CategoryTitle' => $this->machine->PrimaryCategoryPage()->Title,
			'Title' => $this->machine->Title,
			'MachineName' => $this->machine->Title,
			'MachineDescription' => $this->machine->MachineDescription,
			'MachineOverview' => $this->machine->MachineOverview,
			'MachineFeatures' => $this->machine->MachineFeatures,
			'MachineApplications' => $this->machine->MachineApplications,
			'MachineSpecs' => $this->machine->MachineSpecs,
			'MachineOptions' => $this->machine->MachineOptions,
			'MachineNew' => $this->machine->MachineNew,
			'Breadcrumbs' => $this->machine->Breadcrumbs()
		);
		return $this->customise($data)->renderWith(array('MachinePage', 'Page'));
	}