Hi
that’s a bit of a mouthfull. See below.
Also, the htaccess doc is NOT introduced in chapter7. Interestingly, I copied and pasted the chapter7 haccess file it into chapter 6 and ran the index on xampp from chapter 7 using whilst using the chapter6 database
The URLs when clicked all pointed to chapter6 url’s - so somehow the database is also involved in it. In otherwords, when using chapter6 database, the chapter7 home points to the chapter6 database urls and then falls over.
HTACESS file from chapter7 is as follows:
ErrorDocument 404 /index.php
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
</IfModule>
URL PROCESS IS AS BELOW
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.
Manually setting the URL path is something we may need to do, so a simple setter
method is needed.
/**
* Set the URL path
* @param String the url path
*/
public function setURLPath($path)
{
$this->urlPath = $path;
}
The getURLData method processes the incoming URL, and breaks it down into parts,
building up an array of “URL bits”.
/**
- Gets data from the current URL
- @return void
*/
public function getURLData()
{
$urldata = ( isset( $_GET[‘page’] ) ) ? $_GET[‘page’] : ‘’ ;
$this->urlPath = $urldata;
if( $urldata == ‘’ )
{
$this->urlBits = ‘’;
$this->urlPath = ‘’;
}
else
{
$data = explode( ‘/’, $urldata );
while ( !empty( $data ) && strlen( reset( $data ) ) === 0 )
{
array_shift( $data );
}
while ( !empty( $data ) && strlen( end( $data ) ) === 0)
{
array_pop($data);
}
$this->urlBits = $this->array_trim( $data );
}
}
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 specific bit. For example, if the
request is friends/view/ID, the first 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.
public function getURLPath()
{
return $this->urlPath;
}
If we need to generate a URL, for instance, to build a link, or redirect the user, we
can make this easier with a helper function, which takes an array or URL $bits, any
additional information to go in the query string of the URL, $qs, and if the URL is an
administrative URL, $admin, (if it is, then it appends the administration directory to
the URL).
public function buildURL( $bits, $qs, $admin )
{
$admin = ( $admin == 1 ) ? $this->registry->getSetting('admin_
folder') . '/' : '';
$the_rest = '';
foreach( $bits as $bit )
{
$the_rest .= $bit . '/';
}
$the_rest = ( $qs != '' ) ? $the_rest . '?&' .$qs : $the_rest;
return $this->registry->getSetting('siteurl') . $admin . $the_rest;
}