Results 1 to 2 of 2
-
4th Feb 2010, 02:10 AM #1OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comTutorial [Creating a PHP Framework] {advanced} (PART 3)
Part 2: http://www.besthostingforums.com/showthread.php?t=24411
OK so heres part 3 everyone!
In part to we discussed Input and why it is used, today were going to be discussing Output..
So the output side of things will handle all data sent to the user, such as cookies, headers, html and it will also set the compression if needed!
were only going to set up a basic ouput object at first with encoding,headers, and cookies! if you wish you could go more advanced but we keep it simple at the start!
Ok so lets create a new file called Output.php!
in the code below ive left comments so you know whats going on!
PHP Code:<?php
if(!defined('BASE_PATH')){exit;} /*REMEMBER this should be in every file thats included by startup.php*/
class Output
{
private $headers = array(); //This will hold all the headers in
private $compression_level = 0; //This will hold the compression level
private $output = ''; // this will be used to hold the output data.
private $callbacks = array(); // this will hold any callbacks you wish to perform before the data is outputted!
function __construct()
{
//Do nothing in the construct!
//unless you want to add some global headers like content-type but i dont advise it as you will be using this class for all outputs such as xml,js,json,html etc
}
/*
This function will allow you to add callbacks su as $this->addCallback('htmlenities') or $this->addCallback(array($object,'method')) ;
*/
public function addCallback($callback){
//Ok we need to check if the callback is a string or array, if its a string we treat it as function else if its an array we treat as method of an object
switch(gettype($callback))
{
case 'string':
is_callable($callback,false,$callable) ? $this->callbacks[] = $callable : trigger_error('Callback not available ({$callback})',E_USER_ERROR);
break;
case 'array':
//The second param tells php its an object
is_callable($callback,true,$callable) ? $this->callbacks[] = $callable : trigger_error('Callback not available ({$callback})',E_USER_ERROR);
break;
}
//Ok so if you ever get errors from here make sure your functions/methods are callable
}
/*
This function will be used to add headers to the array
*/
public function addHeader($name,$value){
$this->headers[$name] = $value; //Simple eh
}
public function addCookie($key,$value,$expire = 604800,$path = '/',$domain = '',$secure = false,$httponly = true)
{
setcookie($key,$value,$expire,$path,$domain,$secure,$httponly);
}
/*
This will remove any set headers!
*/
public function removeHeader($name)
{
if (isset($this->headers[$name]))
{
unset($this->headers[$name]);
}
}
/*
This will do a redirect with headers and exit just after
*/
public function redirect($url)
{
header('Location: ' . $url);
exit;
}
public function setCompression($level)
{
$this->compression_level = (int)$level;
}
/*
This send function will take the html you wish to output and perform compression, headers, and initial output!
*/
public function send($contents)
{
//Set the variable in the class
$this->output = $contents;
//we going to run the callbacks on the output
foreach($this->callbacks as $this->callback)
{
call_user_func($this->callback,&$this->output); //we use the ampersand to tell php to directly save it on the inputted variable
}
//Lets check to see if we have a compression leve
if($this->compression_level)
{
//we compress with the private::compress function
$this->output = $this->compress($contents,$this->level);
}
//Ok so now we may have headers so we need to send these before we send the data/html
if(!headers_sent())
{
//Aslong as the headers are not set we then loop the keys and values and send them via header();
foreach ($this->headers as $key => $value)
{
header($key . ': ' . $value);
}
}
//So now we just have to echo the output
echo $this->output;
}
private function compress($data, $level = 0)
{
if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE))
{
$encoding = 'gzip';
}
if(isset($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== FALSE))
{
$encoding = 'x-gzip';
}
if(!isset($encoding))
{
return $data;
}
if(!extension_loaded('zlib') || ini_get('zlib.output_compression'))
{
return $data;
}
if(headers_sent())
{
return $data;
}
if(connection_status())
{
return $data;
}
$this->addHeader('Content-Encoding', $encoding);
return gzencode($data, (int)$level);
}
}
?>
Basically so far with what we have you will use the Input to grab what you need! then you will use the database to fetch content, then you will assign it to a template manager and then send the compiled data to the Output with compression and other headers
well thats the principle any way!
Ok so we need to include this into the framework but first
Save the file in /system/engine/Output.php
Then open up the startup.php file and lets include the file!
FInd:
PHP Code:include SYSTEM_BASE_PATH . '/engine/Input.php';
PHP Code:include SYSTEM_BASE_PATH . '/engine/Output.php';
Now lets add it to the Registry system!
Find:
PHP Code:Registry::set('Input', new Input());
PHP Code:Registry::set('Output', new Output());
Part 4 we will be looking at a database extraction layer and how we can use this to perform fast query such as 1 line assoc loops!litewarez Reviewed by litewarez on . Tutorial [Creating a PHP Framework] {advanced} (PART 3) Part 2: http://www.besthostingforums.com/showthread.php?t=24411 OK so heres part 3 everyone! In part to we discussed Input and why it is used, today were going to be discussing Output.. So the output side of things will handle all data sent to the user, such as cookies, headers, html and it will also set the compression if needed! were only going to set up a basic ouput object at first with encoding,headers, and cookies! if you wish you could go more advanced but we keep it simple Rating: 5Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
-
4th Feb 2010, 02:12 AM #2Member
will this help me to get a girlfriend im confused ?
?
na im joking been wanting to learn php will sit down one day a read through these should help me thanks liteSignature too big, removed by staff.
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
Tutorial [Creating a PHP Framework] {advanced} (PART 6) Smarty Engine
By litewarez in forum Tutorials and GuidesReplies: 5Last Post: 19th Feb 2010, 06:38 PM -
Tutorial [Creating a PHP Framework] {advanced} (PART 5) - Sessions
By litewarez in forum Tutorials and GuidesReplies: 1Last Post: 12th Feb 2010, 03:06 PM -
Tutorial [Creating a PHP Framework] {advanced} (PART 4)
By litewarez in forum Tutorials and GuidesReplies: 1Last Post: 9th Feb 2010, 04:24 AM -
Tutorial [Creating a PHP Framework] {advanced} (PART 2)
By litewarez in forum Tutorials and GuidesReplies: 2Last Post: 3rd Feb 2010, 05:37 AM -
Tutorial [Creating a PHP Framework] {advanced} (PART 1)
By litewarez in forum Tutorials and GuidesReplies: 1Last Post: 30th Jan 2010, 03:37 PM
themaLeecher - leech and manage...
Version 5.03 released. Open older version (or...