My 'Content' class

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

My 'Content' class

Post by shiznatix »

For my framework I have a class that runs the controller and view class of the page that I am on. If you want to run a method in the controller, you can either send a GET var called "action=method_name" or a post var called "action[method_name]" the same applies for the view class's method.

Thats all this really does, sees if you want to run an action, then runs said action and then gets the proper view method and sets the $content to the return of that. Let me know what you think or if there is any sort of security gap or whatnot.

Code: Select all

<?php

class content
{
    const DEFAULT_VIEW_METHOD = 'main';

    private $page;
    private $configuration;

    private $view_class;
    private $controller_class;

    private $content;

    public function __construct($page, $configuration)
    {
        $this->page = $page;
        $this->configuration = $configuration;

        if (!array_key_exists($this->page, $this->configuration))
        {
            trigger_error('The page is undefined in the configuration', E_USER_ERROR);
        }

        $controller_method = $this->get_controller_method();

        if (false !== $controller_method)
        {
            $this->load_controller_class();
        }

        $this->load_view_class();
    }

    private function load_controller_class()
    {
        if (
            empty($this->configuration[$this->page]['controller']['location'])
                ||
            !file_exists($this->configuration[$this->page]['controller']['location'])
           )
        {
            return false;
        }

        require_once $this->configuration[$this->page]['controller']['location'];

        $this->controller_class = singleton::get_object($this->configuration[$this->page]['controller']['class_name']);

        $controller_method = $this->get_controller_method();

        if (method_exists($this->controller_class, $controller_method))
        {
            $this->controller_class->$controller_method();
        }
    }

    private function load_view_class()
    {
        if (!array_key_exists($this->page, $this->configuration))
        {
            trigger_error('The page is undefined in the configuration', E_USER_ERROR);
        }

        require_once $this->configuration[$this->page]['view']['location'];

        $this->view_class = singleton::get_object($this->configuration[$this->page]['view']['class_name']);

        if (is_object($this->controller_class) && $this->controller_class->get_view())
        {
            $view_method = $this->controller_class->get_view();
        }
        else
        {
            $view_method = $this->get_view_method();
        }

        if (method_exists($this->view_class, $view_method))
        {
            $this->content = $this->view_class->$view_method();
        }
        else
        {
            $this->content = $this->view_class->main();
        }
    }

    private function get_view_method()
    {
        if (array_key_exists('view', $_GET))
        {
            return $_GET['view'];
        }
        else if (array_key_exists('view', $_POST))
        {
            $view = array_keys($_POST['view']);
            return $view[0];
        }

        return self::DEFAULT_VIEW_METHOD;
    }

    private function get_controller_method()
    {
        if (array_key_exists('action', $_GET))
        {
            return $_GET['action'];
        }
        else if (array_key_exists('action', $_POST) && is_array($_POST['action']))
        {
            $action = array_keys($_POST['action']);
            return $action[0];
        }

        return false;
    }

    private function build_header()
    {
    }

    private function build_footer()
    {
    }

    public function build_content()
    {
        $header = $this->build_header();
        $footer = $this->build_footer();

        return $header.$this->content.$footer;
    }
}

?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I am guessing it is a Page Controller. Where does $page come from in the constructor? Can use show a use case or two?
(#10850)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

All files require_once the engine file which calls the content class. The engine fine looks like this:

Code: Select all

<?php

$start_time = microtime(true);
session_start();

require_once 'required.php';

set_error_handler(ERROR_HANDLER_FUNCTION);
date_default_timezone_set(TIMEZONE);

if (DB_CONNECT)
{
    $db = main_singleton::get_object('db');
    $db->connect(DB_USER, DB_PASS, DB_NAME, DB_HOST);
}

main_singleton::set_variable('configuration', $configuration);

$content = new content('index');

echo $content->build_content();

?>
Post Reply