Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    933
    Thanks
    178
    Thanked 109 Times in 109 Posts

    single entry point php

    Hi guys

    I'm interested to build a single entry point system, but first i'm trying to understand how it all works so I'm doing a bit of research.


    By chance I came across this code (shown below) in another recent thread HERE ,but there are a few of things that I don't understand which you maybe able to help clarify.


    What is ROOT_HTTP and DEFAULT_ACTION?
    Are they constants?
    Is ROOT_HTTP simply '/' or what?
    As they are not declared in the script, where would they be declared?

    Lastly, quote: request::value() or request::value(0) would return "article"

    what is meant by "article" here? Does it mean returns 'an article ie some content' or the word 'article'?


    Any help that helps me understand 'single entry' and how it works is appreciated.

    LT




    PHP Code:
    final class Request {

        private static
            
    $data = [],
            
    $path '';
        
        private static function 
    set() {
            
    self::$path parse_url(
                
    trim(str_replace(['\\''%5C'],  '/'$_SERVER['REQUEST_URI']), '/'),
                
    PHP_URL_PATH
            
    );
            if (
    strpos(self::$path'..')) die('Hacking Attempt Detected, Aborting');
            
    self::$path substr(self::$pathstrlen(ROOT_HTTP) - 1);
            
    self::$data = empty(self::$path) ? [DEFAULT_ACTION] : explode('/'self::$path);
            foreach (
    self::$data as &$p$p urldecode($p);
        }
        
        public static function 
    value($index 0) {
            if (
    count(self::$data) == 0self::set();
            return isset(
    self::$data[$index]) ? self::$data[$index] : false;
        } 
    // Request::value
        
        
    public static function getPath() {
            if (
    count(self::$data) == 0self::set();
            return 
    self::$path;
        }
        
    // Request 

    So if for example you had:
    HTML, CSS and JavaScript Frameworks - Incompetent Nonsense - CUTCODEDOWN

    request::value() or request::value(0) would return "article",
    request::value(1) would return "HTML_CSS_and_JS_frameworks"
    0000

  2. #2
    Regular Coder
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    365
    Thanks
    1
    Thanked 50 Times in 47 Posts
    Oops, when I posted that I didn't copy over my define.

    I have a standard set of DEFINE I usually put in place for certain stock values. Clean a few of them up, extract out needed sub-sections, etc, etc...

    This is the full set lifted straight out of my codebase:
    Code:
    define('SCRIPT_PATH', cleanPath(pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME)));
    define('ROOT_HTTP', '/' . SCRIPT_PATH . (SCRIPT_PATH == '' ? '' : '/'));
    define('ROOT_LOCAL', pathinfo($_SERVER['SCRIPT_FILENAME'], PATHINFO_DIRNAME) . '/');
    define('HOST_PROTOCOL', 'http' . (
    	isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] != 'off') ? 's' : ''
    ) . '://');
    define('HOST_HTTP', HOST_PROTOCOL . $_SERVER['HTTP_HOST']);
    define('BASE_HTTP', HOST_HTTP . ROOT_HTTP);
    define('PATH_HTTP', 
    	'/' . parse_url(cleanPath($_SERVER['REQUEST_URI']), PHP_URL_PATH)
    );
    define('FULL_HTTP', HOST_HTTP . PATH_HTTP);
    define('CANONICAL_PATH', Request::getPath());
    define('CANONICAL_URI', HOST_PROTOCOL . WORKING_DOMAIN . '/' . CANONICAL_PATH);
    define('CANONICAL_URIe', urlencode(CANONICAL_URI));
    You can pick and choose the ones you need. The thing about this code is it parses andd removes the filename from $_SERVER['SCRIPT_NAME'] so we can figure out what our root directory is automatically. This means you can place this in a subdirectory too, if you want to run something different in the hosting root.

    In my template, I generally will include:

    Code:
    echo '<base href="', BASE_HTTP, '">';
    Which makes sure the fact that while the served URI could be something like /articles/articleName, the base href is the root from which the script itself exists....

    Unrelated to doing a single index approach but something I highly recommend, this file:

    /libs/gzip.lib.php
    Code:
    <?php
    /*
    	gzip.lib.php
    	
    	Version 1.0 Jason M. Knight, August 2009
    	
    	Uses a proper exit handler to provide automation of gzip compression of our
    	PHP output with little if any headaches.
    	
    	ASSUMES:
    		CONTENT_ENCODING contains either 'x-gzip' or 'gzip' based on the value in
    		HTTP_ACCEPT_ENCODING. See "defines.php" to see how this is set.
    		
    	If STRIP_WHITESPACE is defined whitespace between tags or at the start of
    	lines will be stripped, as will comments. Whitespace between a tag and
    	CDATA or between attributes will be left alone.
    	
    */
    
    ob_start();
    ob_implicit_flush(0);
    register_shutdown_function(function() { 
    	header('Content-Encoding: ' . CONTENT_ENCODING);
    	$contents = ob_get_contents();
    	if (defined('STRIP_WHITESPACE')) $contents = preg_replace(
    		['#<!--.*?-->#s', '#>\s+<#', '#\n\s+<#'],
    		['', '><', '<'],
    		$data
    	);
      ob_end_clean();
    	echo "\x1f\x8b\x08\x00\x00\x00\x00\x00",
    		substr(gzcompress($contents, 6), 0, -4);
    });
    called via this as the VERY FIRST THING (I'm talking not even whitespace before the <?php) in your index.php:
    Code:
    <?php
    // start compression first since it does a heading and starts buffering
    define('CONTENT_ENCODING', 
    	(strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== false) ? 'x-gzip' :
    	(strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false) ? 'gzip' :
    	false
    );
    if (CONTENT_ENCODING) include('libs/gzip.lib.php');
    Will automatically add proper gzip compression to serving your markup, headache free. You don't need to do anything at the end, it even works if you use die() or for error messages as it uses register_shutdown_function to set the proper encoding, compress the buffer, and adjust the header for maximum compatibility.

    I'm heading to bed and have doctors visits first thing in the morning, but tomorrow evening or wednesday if I can spare the time, I'll toss together a quick working demo for you of how I approach it... I'll basically give you a gutted down copy of my system with docs, since I've been meaning to write documentation explaining it anyways. It'll even have a rudimentary template system in place to make working with the HTML and CSS easier.

    Single entry via a blacklist/whitelist is to a varying extent a swan-dive into the deep end of the learning pool... There's a LOT of ground to cover particularly if one wants to automate it so that you can then focus on what's really important -- CONTENT.
    Last edited by deathshadow; 05-17-2016 at 04:23 AM.
    From time to time the accessibility of websites must be refreshed with the blood of designers and owners; it is its natural manure.
    http://www.cutcodedown.com

  3. Users who have thanked deathshadow for this post:

    low tech (05-17-2016)

  4. #3
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    933
    Thanks
    178
    Thanked 109 Times in 109 Posts
    Hi deathshadow

    Thanks for the very helpful reply and for the offer of extra help. That's way more than I could have expected, but it's very much appreciated and i'm very interested to learn more.

    I have done a lot of reading and experimenting with OOP. I've looked at Concrete5, Laravel and Symphony to gain some understanding of what these frameworks do, not that I want to replicate them. I'm still trying to fully understand MVC.


    Today, I will carefully read through and play around with the code you have offered and really look forward to seeing and learning from your demo code.

    I appreciate the time and effort and definitely do not take it for granted.

    ps. I'm thick skinned, so anything I say or do that's idiotic, feel free let loose and pounce on me in the name of learning:-)
    It's all good.


    LT
    0000


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •