Helpers and Global Objects for building your Phenotype Web Application
Summary
- Phenotype assists you with numerous helper functions and helping global objects
- There are helpers for
- url management
- encoding
- database operations
- request access
Managing URLs
When linking to a page, you should always use the link helper functions. Only thereby you can avoid broken links, when moving or renaming pages. Additionally it’s very easy, so there’s really no reason to not use them:
Linking to pages
- url_for_page($pag_id, $_params=null, $lng_id=null, $smartUID=””, $fullUrl=false)
- title_of_page($pag_id,$lng_id=null)
Example:
$html ='<ul>'; foreach ($_pages AS $pag_id => $level) { $class="level".$level; if(in_array($pag_id,$_path)) { $class.="active"; } $html .= '<li class="'. $class .'"><a href="'.url_for_page($pag_id).'">'.codeH(title_of_page($pag_id)).'</a></li>'; } $html .='</ul>';
Note: You might wonder about increasing database queries when using the url helpers. That’s right at first glance, but this helpers use caching internally, so the number of queries drops significant after a few recurrences. Overall usage of this helpers lowers the load.
- Usage within Smarty templates
This helpers are available for usage in Smarty templates, take a look at the following example:
<a href="{url_for_page pag_id=43}">Imprint</a>
Linking to Content (objects)
- url_for_content($dat_id,$action,$lng_id=null,$_params,$smartUID=””,$fullUrl=false)
- →buildURL($action=“show”,$lng_id=null)
- urlstrip($s,$lowercase=false)
E.g. you have a content class “News” and written an displayment include, embedded on a dedicated page. You may now implement the method buildURL() of your content class to return a valid url for displaying the news. Of course you must have created a page “listening” to that url first.
... public function buildURL($action="show",$lng_id=null) { return "news/" . urlstrip($this->get("title")); } ...
The helper method urlstrip converts any title into a SEO-optmized valid URL by stripping unwanted characters and replacing delimiters with ”-”.
Accessing the Request ($myRequest)
For accessing the Request Phenotype offers the global object $myRequest.
global $myRequest
Please use it instead of the superglobal $_REQUEST array. It automatically revokes any magic quotes disturbancies for you and offers you various getters for filtered request access (for preventing SQL injections and XSS attacks). Additionally this object is fundamental for the Phenotype smartURL feature since it analyzes the request string and provides smartParams.
For all available getters check the API documentation. Here’s a list of the most important ones:
| check($property) | check, if property is set |
| get($property,$default=null) | raw access to value |
| getI($property,$default=null) | get value converted to int |
| getH($property,$default=null) | get value HTML encoded |
| getHBR($property,$default=null) | get value HTML encoded, additionally line breaks are converted to <br/> |
| getD($property, $decimals,$default=null) | get value as number with $decimals decimal places |
| getA($property,$allowedchars=PT_ALPHANUMERIC,$default=null) | get value filtered, only characters within $allowedchars gets through |
Database Access ($myDB)
global $myDB
Phenotype holds the database connection in the global object called $myDB.
When developing a Phenotype application you barely need to do much with the database. So it’s not really a problem, that this class offers you just one important method:
- function query($sql)
This method returns a standard MySQL recordset. For more info about PHP & MySQL read the official PHP.net MySQL documentation.
Note: There are currently 5 tables, that might be relevant for direct access: content_data, pages, media, user and tickets. Direct access means “reading”. Normally there’s no reason for manually create a delete/insert query. Check API of PhenotypePeerStandard and PhenotypeNavigationHelper for alternate ways to access your content, so ideally you don’t need any database operations at all. More infos about the Phenotype Database layout here.
$myPT (Phenotype)
global $myPT
This object represents the Phenotype system. Hence it’s always available … Nevertheless you won’t need it that often. A few usages are worth to be mentioned here. For everything else check the API documentation.
Buffer management
Use the method startBuffer() to start Output Buffering and stopBuffer() to get the collected output. Don’t use the default php functions, since they could easily interfere with Phenotype buffer management for redirections and debugging.
- function startBuffer()
- function stopBuffer()
Clearing the cache
When editing any content in the backend, all cached objects are cleared momentarily. That is to minimize caching irritations.
If you change relevant content with your script, you might want or have to clear the cache yourself:
global $myPT; $myPT->clearCache();
As simple as possible…
Displaying sequence of page components
- function displaySequence($pag_id,$ver_id,$dat_id_content,$block_nr,$editbuffer=0,$usr_id=0)
This methods enables you to display page components of any page or from any content object record (that used form_sequence).
$myPage
global $myPage
Since Phenotype works page oriented, you will always have an object represting the current page. Following table lists properties of that object, that might be valuable for you. For more info check the API documentation.
| $id | ID of current page |
| $lng_id | language of current page |
| $ver_id | ID of current pageversion |
| $ver_nr | number of current pageversion |
| $lay_id | ID from the page layout |
| $pos | position of the page (related to pages of same level) |
| $pag_id_mimikry | identical to $id or ID of assigend page, if mimikry is configured |
| $pag_id_top | ID of parent page, 0 if on top level |
Accessing Page variables
If a page has page variables you can access them with the default Phenotype getters, like $myPage→get(), $myPage→getI(), $myPage→getH(), $myPage→getHBR() and so on.
During lifetime of a request you may also set variables of page (e.g. for communication between includes) using $myPage→set($property,$vale). Keep in mind those values are temporary and not stored anywhere.
Special methods
The page object offers methods to provide you information about the current page and manipulate them during rendering. Check the API for all of them. Here’s a list of the most valuable ones:
| hasChilds($status=0) | returns number of subpages of a current page or false |
| setTitle($title) | changes the title of a page within an include. That works even if the page itself got cached. To access use smarty variable {$title}. |
$myApp (Application)
global $myApp
Every Phenotype application has one application object prepared to hold appliation specific functions and configurations. For more info on that read here. This object is accessible global as $myApp.
$mySUser (Backend Session User)
global $mySUser
If you change or enhance any backend functionality you can access the current backend user with the global object $mySUser. It is an instance of the PhenotypeUser class, see User & Roles for mor info about Phenotype Users.
Continue with introductive documentation, reading about content objects ...
You think Phenotype Wiki/Documentation could be better?
We too. Please contribute: Edit this page
Recent Blog Posts
- Finally Multibyte - Phenotype 3.0
- Phenotype 2.9 explained
- Integration of PHPIDS (PHP-Intrusion Detection System)
- New Release: Phenotype 2.8 Ready for Download
- Restart: New Phenotype Website Live
- Phenotype worth: 2 Million $
- New Feature: Automatic Image Version Creation
- Additional smartURL variable: smartPATH