Adding subpages to a page automatically from controller

Permalink
Hello everyone, I am new user of C5 and loving what I've seen so far. I have run into a problem and would appreciate some assistance.
Let me describe my case:

I have a single page "projects" and a page type "project".
What I want is when adding a project, to be automatically added as a subpage to projects. I know I could go to projects and add a subpage there however I want this to happen automatically.

I have added a custom controller in controllers/page_types/project.php with an add() method. I was thinking using the cID of project in order to add new projects as a subpage. However I can't seem to figure it out.

Is there some easier way to achieve this effect ?

Thank you in advance

 
fsdd replied on at Permalink Reply
I realized that maybe the way to achieve this is to create a new model

models/project.php

which will extend Page and override the add() method using the cID of projects as cParentID
and loading it from the controller with

controller/page_types/project.php

Loader::model('project');

Is this the right way to go ?
JohntheFish replied on at Permalink Reply
JohntheFish
There are several marketplace packages that set up massive structures of pages for data storage, sometimes even if the pages are never actually visible of the site. Discussion forums, ecommerce, events, calendars, blogs. Maybe the code from the standard C5 blog will give you some ideas.
xaritas replied on at Permalink Reply
You want to get the page that is currently being viewed, and then invoke its add method:

$this = Page::getCurrentPage();
$myPageType = CollectionType::getByHandle('project');
$textHelper = Loader::helper('text');
$pageData = array('name' => $projectName,
                        /* 'cHandle' => $textHelper->sanitizeFileSystem($projectName), */
                         'cDescription' => "The home page of $projectName!");
$childPage = $thisPage->add($myPageType, $pageData);


The 'cHandle' is the slug for the page that will be used to generate the URL. The helper class $textHelper is being used to make a URL-safe name from the project name, in case you don't want to make your users think of one. I commented it out here, because strictly speaking, it's optional--if you don't explicitly pass a cHandle in, Page::add will apply exactly the same transformation to name in order to come up with a cHandle.

So, in sum, yes, if you invoke a method on your single page controller that calls this code, you will indeed generate a sub-page for it.

Edit: fixed spelling mistake that I would never, ever maek