Multi-page Pages (FINALLY!)

Monday, August 2nd, 2010|Beginner tips, Brainstorming|by Romans

Up till now we had to either pile up functions in API class formatted as page_user, page_user_add, page_user_del, or we had to create all those classes, each in it’s own file.

Well – not anymore. Now any page class can have sub-pages. Here how it works:

Sub-pages for pages

You can now define sub-pages inside a page class. System will attempt to class with exact name first, but if now found, it will try shorter classes. For instance, if you call for page page_data_edit_subpage_test:

  • looks for file page/data/edit/subpage/test.php
  • looks for file page/data/edit/subpage.php and function page_test()
  • looks for file page/data/edit.php and function page_subpage_test()
  • looks for file page/data.php and function page_edit_subpage_test()
  • fails with exception that page/data/edit/subpage/test.php is not found (current behavior)

Here is how can you use the function:

[php]
function page_data_edit(){
$f=$this->add(‘Form_Data’)->setSource(‘data’);
// etc
}

[/php]

Initialization

While looking for pages, classes are not being initialized, however once the appropriate class was found, it will be $api->add()’ed. That means that init() will be fired too. So if you want any common code you want for all sub-pages to be executed, then you should place it there.

In regards from the main page – if there is some code you want to have in your “top-most” page, you should put it inside page::initMainPage();

Naming

initMainPage() is being similar to api::initLayout() while page_test have a similar form to Api::page_test format functions.

The Benefit

Main benefit is that this will allow us to create multi-functional pages such as “entity manager”. Here is a short sample:

[php]
<?php
class Page_EntityManager extends Page {
function initMainPage(){
parent::initMainPage();
$g=$this->add(‘MVCGrid’)->setController($this->controller);
$g->addColumnPlain(‘expander_widget’,'edit’,'Edit’);
}

function page_edit(){
$this->api->stickyGET(‘id’);
$f=$this->add(‘MVCForm’)->setController($this->controller);
$this->controller->loadData($_GET['id']);
if($f->isSubmitted()){
$f->update();
$f->js()->redirect($this->api->getDestinationURL(‘..’))
->execute();
}
}
function page_delete(){
$this->controller->loadData($_GET['id'])->delete();
$f->js(true)->redirect($this->api->getDestinationURL(‘..’));
}
}
[/php]

Further from here, you should only inherit your page from Page_EntityManager and set controller.

[php]
<?php
class page_users extends Page_EntityManager {
function init(){
parent::init();
$this->controller=$this->add(‘Controller_User’);
}
}
[/php]

Final Thoughts

I am wondering about one thing. When we inherit class like that, what if we want to disable certain page. One thing is that we could throw some exception. throw new PathFinder_Exception(‘blah’) – would work out properly and bypass the page, but it would be initialized already. Well – I’m not sure about this yet.

3 Comments

romans
Posted August 2, 20106:51 pm

Oh – I must note that this change is only for ApiFrontend. If his works out fine, i’ll also port it into ApiAdmin.

zak
Posted August 18, 20108:30 am

I indeed was tired of creating so much files for each edit :) .. This is a good solution.

[...] Multi-page Pages (FINALLY!) [...]