Le petit framework (or la petite…?)

Home Forums General Chat Le petit framework (or la petite…?)

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #3474
    Joaco
    Participant

    After reading this(tl;dr: why and how you should create your own framework), I tried to create my own framework.

    So, eh, it works like this:
    The directory tree looks something like this:


    index.php

    classes/
    classes/Database.php
    classes/MessageHandler.php
    classes/Template.php
    classes/URL.php
    css/
    css/styles.css
    pages/
    pages/Main.php
    pages/SamplePage.php
    templates/
    templates/header.html
    templates/footer.html
    templates/index.html
    templates/sample.html

    First, the server analyzes the URL. And uses the following format: host/index.php/class/method/parameter1 (/parameter2/parameter3/parameter4, etc.).
    To do this, it uses the class URL.php located on /classes/URL.php:

    <?php
    /* /classes/URL.php stores class URL.php
    //Define constants to be used. You may customize these.
    define("BaseURL","/Blomie/"); // The location of the project folder
    define("DefaultClass","Main");
    class URL
    {
    public $Class;
    public $Function;
    public $Parameters;
    public function URL() {
    $URL = str_ireplace(array(BaseURL,'index.php/','index.php'),'',$_SERVER['REQUEST_URI']);
    $this->Parameters = array();
    if(strpos($URL,'/') !== false)
    {
    //case /Blomie/index.php/Class/Function
    $URL = explode("/",$URL);
    $this->Class = $URL[0];
    $this->Function = (count($URL)>1 && @$URL[1] != '')? $URL[1]:"Render";

    if(is_numeric($this->Function)) { // when you call Blomie/Blog/23 this will translate it into /Blog/Render/23
    $this->Function = 'Render';
    for($x=1;$x<count($URL);$x++) {
    $this->Parameters[] = $URL[$x];
    }
    } else if(count($URL)>2){
    for($x=2;$x<count($URL);$x++) {
    $this->Parameters[] = $URL[$x];
    }
    }

    } else {
    //handles case /Blomie/index.php/Class AND case /Blomie/index.php
    $this->Class=($URL != '')? $URL:DefaultClass;
    $this->Function="Render";
    }
    }
    }

    ?>

    This class, when initialized, analyses it according to the rules specified above. It has special cases, though:
    When the url is, say, example.com/index.php/Blog/23, if it followed the rules, the function would be 23! But you can’t have function names with numbers only!
    So, what this class does is: calls the default method on every class (called “Render”) and passes 23 and all the following as parameters. This means that, if you access example.com/index.php/Blog/23/Wild_Night/Le_petit_nicolas/1337, it will invoke the class Blog, call its method Render, and pass the following arguments: 23,Wild_Night,Le_petit_nicolas and 1337.
    On the case that the url is example.com/ or example.com/index.php : calls the default class specified on the constant “DefaultClass” at the beginning of the file.
    Actually, this class only stores the information, in the public properties $Class, $Function and $Parameters. String, String and Array, respectively.
    index.php calls it:


    (...)

    include("classes/URL.php");

    (...)

    $URL = new URL();

    (...)

    if(!file_exists("pages/".addslashes($URL->Class).".php"))
    {
    trigger_error("The page you requested does not exist.");
    } else
    {

    include("pages/".addslashes($URL->Class).".php");
    $ClassName = $URL->Class;
    $$ClassName=new $ClassName;
    call_user_func_array(array($$ClassName,''.$URL->Function),$URL->Parameters);
    }

    What this does is:
    if it exists, include the file ‘pages/Blog.php’. The class should be the same as the filename without the extension. If it exists, initialize an instance of the class in the variable $Blog (the name of the variable changes depending on the name of the filename w/o the ext.) ($Blog = new Blog;). If it exists, call the method specified on the URL, or, on default, ‘Render’.

    So, you may say, ‘but thsi shit is too complex how the fuck is this supposed to speed up the thing omgdsofjgasgijasfiaopsfjaisofj’

    the class template.php is a clone of template_engine used on phpbb.

    Sample page on page/My_Page.php


    class My_Page
    {
    public $Handle;
    public function Render()
    {
    global $Template;
    $Template->set_filenames(array(
    'my_page'=>'my_page.html'
    ));
    MessageHandler::HandleAnnouncement("We're on alpha! :D Report any bugs you see, please!");
    $Template->assign_var('name','VuTales');
    $this->Handle='my_page';
    }
    }

    and, on templates/my_page.html:


    {header}

    Hello, {name}!


    {footer}

    produces the following output:





    Untitled


    We're on alpha! :D Report any bugs you see, please!

    Hello, VuTales!




    {header} and {footer} are already specified on index.php and are actually variables from handles ‘templates/header.html’ and ‘templates/footer.html’, respectively.

    On the future, I shall:
    expand the database class
    allow for a custom default method depending on a public property called ‘DefaultMethod’ of the class
    etc

    ill later expand this and probably include the files, right now I should be studying latin.

    #21314
    Gujju
    Participant

    More technobabble that i dont understand. 🙂

    #21317
    DarkDragoon
    Participant

    …If only I could waste enough time to go through this.

    Is there a point to the madness?

    #21320
    Nass
    Participant

    Yeah, why don’t you just PM someone like BryBry or David about this stuff. Cuz I doubt anyone else really cares that much.

    Edit: Yes, I know there’s a “Computing” section. But I’m just saying the truth here.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.