Page 2 of 2

Re: Front controller by Corbyn, where to put website wrapper?

Posted: Sun Apr 13, 2008 5:44 pm
by blueyon
Everah | Please use bbCode tags when posting code in the forums. It makes it a ton easier to read for everyone else.

My idea is using the render method in the page controller to trigger a layout pre setup in a config file.

Example:

Code: Select all

<?php
$this->assign('title', 'My Home Page');
$this->render('default');
 
$cfg['default'] = array(
    'format' => 'xhtml',
    'layout'   => 'layout'
    'children' => array(
        'header',
        'footer'
    )
);
?>
 
Everah | Please use bbCode tags when posting code in the forums. It makes it a ton easier to read for everyone else.

Re: Front controller by Corbyn, where to put website wrapper?

Posted: Mon Apr 14, 2008 11:44 am
by RobertGonzalez
Please use code highlighting when posting code in the forums. Even for simple code samples it makes it way easier to read. Thanks.

Re: Front controller by Corbyn, where to put website wrapper?

Posted: Mon Apr 14, 2008 5:37 pm
by Chris Corbyn
blueyon wrote:Everah | Please use bbCode tags when posting code in the forums. It makes it a ton easier to read for everyone else.

My idea is using the render method in the page controller to trigger a layout pre setup in a config file.

Example:

Code: Select all

<?php
$this->assign('title', 'My Home Page');
$this->render('default');
 
$cfg['default'] = array(
    'format' => 'xhtml',
    'layout'   => 'layout'
    'children' => array(
        'header',
        'footer'
    )
);
?>
 
Everah | Please use bbCode tags when posting code in the forums. It makes it a ton easier to read for everyone else.
layout.php should be responsible for including the header and footer I'd say. Having it as a config option just adds unneeded complexity.

Re: Front controller by Corbyn, where to put website wrapper?

Posted: Tue Apr 15, 2008 6:28 am
by blueyon
layout.php should be responsible for including the header and footer I'd say.
It depends on how you set the pages. Anyway it was just an example.
Having it as a config option just adds unneeded complexity.
Not really! If you have a routing class and the routing class has a config file for all the different routes why not have one for the view.

I think ROR does it this way:

Code: Select all

 
<?php
$this->render($option);
?>
 
Possible options you could have:
 

Code: Select all

 
<?php
$option['template'] = 'default.tpl'; // Template to use
$option['id']           = 'content';     // Var name to use with layout
$option['layout']     = 'layout1';     // Layout controller to attach rendered template to
$option['children']  = array();       // Children to load
$option['engine']    = 'smarty';      // Template engine to use
$option['format']    = 'xhtml';       // Ouput format to use could be XHTML, PDF, etc.. (Could be set to automatically add header into the response)
?>
 
If you have a separate config file you can load different children up with differn't layouts. If it was coded in the layout controller you would have to create a new layout everytime you wanted to show child controllers on some pages and not others.

Re: Front controller by Corbyn, where to put website wrapper?

Posted: Tue Apr 15, 2008 8:21 am
by kilbad
@blueyon - I like what you are outlining, and wanted to know if you could give me a fuller example to work with? Basically, I want to have a config file with a bunch of default setting, but I want to be able to override those setting if a non-default option is provided. How would I do that?

Re: Front controller by Corbyn, where to put website wrapper?

Posted: Tue Apr 15, 2008 12:02 pm
by blueyon
This is just a bit of an example.

Its not finished and the coding in the render method is a bit sloppy.

Code: Select all

 
<?php
class PageController {
    protected $registry;
    protected $id;
    protected $template; 
    protected $layout;
    protected $children = array();
    protected $data     = array();
    
    public function __construct($registry) {
        $this->registry = $registry;
    }
    
    public function __get($key) {
        return $this->registry->get($key);
    }
    
    public function __set($key, $value) {
        $this->registry->set($key, $value);
    }
    
    public function getId() {
        return $this->id;
    }
        
    public function assign($key, $value) {
        $this->data[$key] = $value;
    }
            
    protected function forward($class, $method, $args = array()) {
        return new Action($class, $method, $args);
    }
    
    protected function redirect($url) {
        header('Location: ' . $url);
        exit();
    }
 
    protected function render($option = array()) {
        foreach ($option as $key => $value) {
            $this->$key = $value;
        }
    
        foreach ($this->children as $child) {
            $obj = $this->create($child);
                
            $forward = $obj->execute();
            
            if ($forward) {
                return $forward;
            }
                
            $this->data[$child] = $obj->getOutput();
        }
        
        if ($this->template) {
            $this->output = $this->fetch($this->template);
        }
        
        if ($this->layout) {
            $obj = $this->create($this->layout);
            
            $obj->data[$this->id] = $this->output;
                
            $forward = $obj->execute();
                
            if ($forward) {
                return $forward;
            }               
        }
        
        $this->response->setOutput($this->output);
    }
 
    function fetch($filename) {
    
    }
    
    private function create($controller) {
        $file  = DIR_CONTROLLER . $controller . '.php';
        $class = 'Controller' . $controller;
        
        if (file_exists($file)) {
            include_once($file);
            
            return new $class($this->registry);
        } else {
            exit('Error: Could not load controller ' . $controller . '!');
        }
    }
 
    public function getOutput() {
        return $this->ouput;
    }
} 
?>