Planning and Developing the Core Framework
[ 52 ]
* @param String data : if the tag value = condition then extra tag
is replaced with this value
*/
public function addAdditionalParsingData($block, $tag, $condition,
$extratag, $data)
{
$this->apd[$block] = array($tag => array('condition' => $condition,
'tag' => $extratag, 'data' => $data));
}
We will want to get a list of all the template bits we need to process into the page
(processing is done by the template object).
/**
* Get the template bits to be entered into the page
* @return array the array of template tags and template file names
*/
public function getBits()
{
return $this->bits;
}
We also need to get our array of additional parsing data for the template handler
to process.
public function getAdditionalParsingData()
{
return $this->apd;
}
We often need to just access a specic loop block within our page; this method
makes this easy, by searching for us using regular expressions, and returning it.
/**
* Gets a chunk of page content
we would either have to explicitly remove the template tags, or instead, prex them
with form_, and any leftovers are auto removed.
Once this is done, the content is returned to be output to the browser.
public function getContentToPrint()
{
$this->content = preg_replace ('#{form_(.+?)}#si', '',
$this->content);
$this->content = preg_replace ('#{nbd_(.+?)}#si', '',
$this->content);
$this->content = str_replace('</body>', '<! Generated by our
Fantastic Social Netowk >
</body>', $this->content );
return $this->content;
}
Authentication
In Chapter 3, Users, Registration, and Authentication, we will discuss how our user's
database will be structured, how we will manage the login and sign up process,
and how user authentication will work in general.
For now, we will leave this aspect out of our registry, and come back to it in the
next chapter.
URL processing
Since we are using a single frontend controller, we need to process the incoming
URL, in particular the page $_GET variable, to work out how to handle the users
request. This is generally done by breaking the variable down in parts, separated
by a forward slash.
This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010
3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246
Download from www.eBookTM.com
Planning and Developing the Core Framework
[ 54 ]
array_shift( $data );
}
while ( !empty( $data ) && strlen( end( $data ) ) === 0)
{
array_pop($data);
}
$this->urlBits = $this->array_trim( $data );
}
}
This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010
3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246
Download from www.eBookTM.com
Chapter 2
[ 55 ]
The rest of our social networks code needs to access the URL bits to determine what
they need to do, so we need a suitable get method.
public function getURLBits()
{
return $this->urlBits;
}
Similarly, we may need to have easy access to a specic bit. For example, if the
request is friends/view/ID, the rst bit would indicate that we use the friend's
controller; the friends controller would then use a switch statement against the
second URL bit, to work out what it needs to do.
public function getURLBit( $whichBit )
{
return ( isset( $this->urlBits[ $whichBit ] ) ) ?
$this->urlBits[ $whichBit ] : 0 ;
}
Another getter we need is to get the URL path.
• Accessing the le system
• Enhancing security:
° Checking against a banned list
° Checking the format of certain data
• Generating and processing RSS feeds
Front Controller: single point of access
As we discussed earlier, we are going to implement the Front Controller pattern. This
will provide us with a single point of access to the framework powering Dino Space.
index.php
Our front controller is our index.php le. The rst thing we should do is call
session_start, as this needs to be done before anything is sent to the browser,
so by calling it rst, we know this will be the case.
session_start();
We should also dene a framework path constant, so if we are in another le
elsewhere, and we need to access a le relative to the framework path, we can
use this constant. Overuse of constants isn't recommended, however, and we
are only going to use them on occasions where appropriate.
DEFINE("FRAMEWORK_PATH", dirname( __FILE__ ) ."/" );
Next, we need to build our registry, and tell it which objects to create. As you can
see, the authenticate object is commented out, until we discuss this in Chapter 3.
require('registry/registry.class.php');
$registry = new Registry();
// setup our core registry objects
$registry->createAndStoreObject( 'template', 'template' );
$registry->createAndStoreObject( 'mysqldb', 'db' );
//$registry->createAndStoreObject( 'authenticate', 'authenticate' );
$registry->createAndStoreObject( 'urlprocessor', 'url' );
This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010
3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246
www.zshareall.com
$registry->getObject('template')->buildFromTemplates('header.tpl.php',
'main.tpl.php', 'footer.tpl.php');
$registry->getObject('template')->parseOutput();
print $registry->getObject('template')->getPage()-
>getContentToPrint();
*/
?>
This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010
3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246
Download from www.eBookTM.com
Planning and Developing the Core Framework
[ 58 ]
.htaccess
We are routing all of our requests through our index.php le, and by passing
further information as the page $_GET parameter, this results in URLs which look
like: http://test.com/?page=friends/view/1/Michael_Peacock. This isn't a
particularly nice looking URL, so we use the Apache
mod_rewrite module (which
most hosts have installed by default—if you use WAMPServer for development, you
may need to enable it in the Apache Modules menu), to take a nicer URL such as
http://test.com/friends/view/1/Michael_Peacock
, which eliminates the
need for ?page=, and translates it into the other format. This is rewritten by
dening a rewrite rule in a
.htaccess le within our code.
ErrorDocument 404 /index.php
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on
[ 59 ]
Summary
In this chapter, we have discussed a number of best practice techniques for designing
a framework to facilitate rapid development of our social networking website. This
included the Model-View-Controller architectural design pattern, the Registry
pattern, the Front Controller pattern, the Factory pattern, and we also discussed
the Singleton pattern.
We also discussed a suitable directory structure for our social networking site to use,
before building the core objects for our registry, and our front controller.
We now have a simple, lightweight framework that can help us rapidly develop
the rest of our social networking site. In the next chapter, we will look at
user registration, logging in, authentication (which will involve creating our
authentication registry object), and a controller to facilitate user registration.
This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010
3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246
Download from www.eBookTM.com
This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010
3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246
Download from www.eBookTM.com
Users, Registration, and
Authentication
With our basic framework in place, we are now able to start developing our social
networking site. The most important aspect of a social networking website is the
users; without users, we don't have a social network. In order to have users who
can use the site (overlooking marketing, and getting users to the site for the
moment), we need to be able to allow users to sign up, log in, and get the details
of a user who is currently logged in. We will also want to be able to manage
permissions of users, to see what they are permitted to do, and what they are
prohibited from doing on the site.
In this chapter, you will learn: