SEO URL TOOLS - The Simple way to get pretty URL's on your PHP site

Examples of Library usage

Lets see how library works on examples. To make things simpler, We will use predefined URL, not URL of current document, but when you unpack these examples on your host, you may easily change it to work on your current URL.

Ok, lets define some rules for our new site.

A) each site option is defined through path parameter named 'opt'. It will contain id of option we want to show. Default item is 'news'

B) if option uses additional parameter its name is 'item'

C) current option has additional query parameter named 'disp' with value 'full'

D) Some documents may be long so they are separated by pages. What page to display is provided through URL option named 'page'. It is numeric and first option.

E) Site is multi language. Language ID is provided through URL option named 'lang'. It may have values 'en', 'it' and 'de' for three languages. This is the second option.

F) Site has two sections: 'public' and 'admin'. Public section is default and is used for presenting site to site visitors. Admin section is used just for admins.

Ok, lets take some example of url to site document:

pretty url: http://mysite.com/news/12/disp=full.3.en

query url: http://mysite.com/?opt=news&item=12&page=3&lang=en&disp=full

This means: show my site, document is news, and news item id is 12. Show third page and use english language. Pay attention that in pretty url, values are sorted by order and in query url they are sorted by name. No matter what URL you use, SEO URL TOOLS will know how to interpret and give you exact values.

Lets create script. First we will include library and set basic parameters as we stated:

<?php
// load library 
include ('../class_seo_url.php');

// initialize 
$seo = new seo_url_tools();
 	
// set seo mode to pretty url mode 
$seo->set_pretty_url_mode (true);
 	
// define sections public and admin with public as default 
$seo->set_sections ('public,admin', 'public');

// define path names opt and item 
$seo->set_path_names ('opt,item');

// define option page, which is numeric as first option 
$seo->set_option ('page', 'numeric');

// define option lang, which is char, with default value 'en' and possible values 'en', 'it' and 'de' 
$seo->set_option ('lang', 'char', 'en', 'en,it,de');

// set default option
$seo->set_default_path_value ('news');

?>

This code defined url parsing behavior. After this is done, we may process url's. Now, we will do some url manipulation. As we said instead of using current URL we will use static predefined example.

<pre>
 <?php
 
 // we will use url example
 	$m_url = "http://mysite.com/news/12/disp=full.3.en";
 	
 	// but you may also use current url by uncommenting following line
 	//$m_url = $seo->get_current_url ('string');
 	
 	//lets parse string url into array structure
 	$m_parsed_url = $seo->url2array ($m_url);
 	
 	// and display what we got
 	print_r ($m_parsed_url);
 	
 	?>
</pre>

This code sets test url and parses it to array structure. Array structure ($m_parsed_url) value is:

Array  (
      [scheme] => http
      [host] => mysite.com
      [doc] => /
      [path] => Array
          (
              [opt] => news
              [item] => 12
          )
      [query] => Array
          (
              [disp] => full
          )

      [options] => Array
          (
              [page] => 3
              [lang] => en
          )
    )

I guess you would trust me that the we would get same array structure if we used query mode and query url example.

Well, this part of the script shows that url is parsed properly. You get data in form that is easy to read, and indeed you may use it directly as it is. However, there is a function get_url_param () which you may use to read data form this structure without knowing exact data structure. It is advisable that you read data through this function, since it is possible that array structure may be changed in the future versions of the library.

So, let's see how to read specific information form parsed URL:

<pre>
<?php
// read host
$m_host = $seo->get_url_param ($m_parsed_url, 'host');
echo "m_host = $m_host\n";

// read section
$m_section = $seo->get_url_param ($m_parsed_url, 'section');
echo "m_section = $m_section\n";

// read opt
$m_opt = $seo->get_url_param ($m_parsed_url, 'path', 'opt');
echo "m_opt = $m_opt\n";

// read item
$m_item = $seo->get_url_param ($m_parsed_url, 'path', 'item');
echo "m_item = $m_item\n";

// read page
$m_page = $seo->get_url_param ($m_parsed_url, 'options', 'page');
echo "m_page = " . (int)$m_page . "\n";

// read lang
$m_lang = $seo->get_url_param ($m_parsed_url, 'options', 'lang');
echo "m_lang = $m_lang\n";

// read disp
$m_disp = $seo->get_url_param ($m_parsed_url, 'query', 'disp');
echo "m_disp = $m_disp\n";

?>
</pre> 

Pay attention that if you read item that is not defined is array structure, you will get default value for that item. This code will produce output:

m_host = mysite.com
m_section = public
m_opt = news
m_item = 12
m_page = 3
m_lang = en
m_disp = full

Now when we know how to read items of url, and I hope you find it pretty easy. It is time to go further. Lets alter URL. For instance, if site is multilingual, we should have links on page to allow visitor to simply change the language of the current document. This means we have to change lang parameter. Let's do it. We will use function url_add_param() :

<pre>
<?php

// change lang option to de
$m_change['options']['lang'] = 'de';

// replace lang parameter in url
$m_new_url = $seo->url_add_param ($m_parsed_url, $m_change);

// we have value as array so we have to convert to string to display
$m_new_url_string = $seo->array2url ($m_new_url);

echo "m_new_url_string = $m_new_url_string\n";
?>
</pre> 

Pay attention that in this example we replaced just one item, by defining array structure containing that item and new value. We could include more items in the same structure and thus change all of them at once. Lets see output of this code:

m_new_url_string = http://mysite.com/news/12/disp=full.3.de

Remember that add_url_param() has three uses: it may add new item value, replace existing item value and delete existing value. If we set replacement value for item, if item does not exist in URl, it will be added. If item exists, value will be replaced with new one. But. If we replace existing item value with empty value, that would actually remove that item from the url.

This kind of item value replacement is used when we just want to replace value of specific item and keep values of others. In our example, that is exactly what we needed, to keep everything but replace language value.

In other situations we may want to remove some items form url while setting some item value. Good example is menu. Menu option changes document we load. Thus, when we change option we should also erase all url items that are specific for that option and eventually replace them with new values. When we create menu option we have to remove path and query parameters but keep language. Lets do that:

<pre>
<?php

// change opt item value to products (we want to show products)
unset ($m_change);
$m_change['path']['opt'] = 'products';
$m_change['path']['item'] = 'cat3';

// replace parameters in url and erase path and query
$m_new_url = $seo->url_add_param ($m_new_url, $m_change, 'params');

// we have value as array so we have to convert to string to display
$m_new_url_string = $seo->array2url ($m_new_url);

echo "m_new_url_string = $m_new_url_string\n";
?>
</pre>

Output is:

m_new_url_string = http://mysite.com/products/cat3.3.de

All parameters are removed so we have clean url to menu option.

And for the last, each site page has to contain link to home page. SEO URL TOOLS provides function that would give you that url, considering all the settings.

<pre>
<?php

echo "site_root = " . $seo->get_root_url($m_new_url) . "\n";

?>
</pre> 

This outputs value for the link to root of the site:

site_root = http://mysite.com/news.3.de

News page is set as default page for the sited since current language is de, which is not default, function provided correct link to the home page in de language.


Built on DevCMS 0.1.4 and SEO URL Tools 1.0.38 - ©2007 Copyright by Predrag Supurović, ©2007 Copyright by DataVoyage