Heb jij lelijke URLs en wil je die wat mooier maken?
Dus van oid "topic.php?id=15" kan je maken: "/topic/15"
Hoe doe je dat? Maak eerst een bestand urlDispatcher.class.php met de class erin:Zet dit in je .htaccess:
Die je dan kan includen op de paginas waar je m wilt gebruiken.
Hieronder een voorbeeld:Veel plezier ermee, laat wel mijn copyright staan svp
!
Raoul
Dus van oid "topic.php?id=15" kan je maken: "/topic/15"
Hoe doe je dat? Maak eerst een bestand urlDispatcher.class.php met de class erin:
Spoiler tonen
[COLOR= #0000BB]<?php[/COLOR]
[COLOR= #FF8000]/**
* Class om URLs af te handelen
* @author Raoul Van den Berge
* @copyright 2012
*/[/COLOR]
[URL="http://www.php.net/class"]class[/URL] UrlDispatcher [COLOR= #007700]{[/COLOR]
[COLOR= #FF8000]/**
* In deze variable staan alle delen van de URL.
* @access private
*/[/COLOR]
[URL="http://www.php.net/private"]private[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #0000BB]$parts[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/Array"]Array[/URL][COLOR= #007700]();[/COLOR]
[COLOR= #FF8000]/**
* Functie om alle delen te verkrijgen en in te stellen voor gebruik.
*/[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] init[COLOR= #007700]() {[/COLOR]
[COLOR= #0000BB]$url[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/explode"]explode[/URL] [COLOR= #007700]([/COLOR] [COLOR= #DD0000]/[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$_SERVER[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]REQUEST_URI[/COLOR][COLOR= #007700]] );[/COLOR]
[URL="http://www.php.net/unset"]unset[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$url[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]0[/COLOR][COLOR= #007700]],[/COLOR] [COLOR= #0000BB]$url[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]1[/COLOR][COLOR= #007700]]);[/COLOR]
[COLOR= #0000BB]$parts[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/Array"]Array[/URL][COLOR= #007700]();[/COLOR]
[URL="http://www.php.net/foreach"]foreach[/URL][COLOR= #007700]([/COLOR] [COLOR= #0000BB]$url[/COLOR] [COLOR= #007700]as[/COLOR] [COLOR= #0000BB]$part[/COLOR] [COLOR= #007700]) {[/COLOR]
[COLOR= #0000BB]$parts[/COLOR][COLOR= #007700][] =[/COLOR] [COLOR= #0000BB]$part[/COLOR][COLOR= #007700];
}[/COLOR]
self[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR][COLOR= #0000BB]$parts[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$parts[/COLOR][COLOR= #007700];
}[/COLOR]
[COLOR= #FF8000]/**
* Functie om een deel uit de URL te verkrijgen.
* @param Integer
* @return String
*/[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] getPart[COLOR= #007700]([/COLOR] [COLOR= #0000BB]$part[/COLOR] [COLOR= #007700]) {[/COLOR]
[URL="http://www.php.net/return"]return[/URL] self[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR][COLOR= #0000BB]$parts[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]$part[/COLOR][COLOR= #007700]];
}[/COLOR]
[COLOR= #FF8000]/**
* Functie om te kijken of een deel van een URL wel bestaat
* @param Integer
* @return Boolean
*/[/COLOR]
[URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] partExist[COLOR= #007700]([/COLOR] [COLOR= #0000BB]$part[/COLOR] [COLOR= #007700]) {[/COLOR]
[URL="http://www.php.net/if"]if[/URL] [COLOR= #007700]([/COLOR] [URL="http://www.php.net/isset"]isset[/URL][COLOR= #007700]([/COLOR] self[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR][COLOR= #0000BB]$parts[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]$part[/COLOR][COLOR= #007700]] ) ) {[/COLOR]
[URL="http://www.php.net/return"]return[/URL] true[COLOR= #007700];
}[/COLOR] [URL="http://www.php.net/else"]else[/URL] [COLOR= #007700]{[/COLOR]
[URL="http://www.php.net/return"]return[/URL] false[COLOR= #007700];
}
}
}[/COLOR]
UrlDispatcher[COLOR= #007700]::[/COLOR]init[COLOR= #007700]();[/COLOR]
?>Options FollowSymLinks MultiViews Die je dan kan includen op de paginas waar je m wilt gebruiken.
Hieronder een voorbeeld:
Spoiler tonen
[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/require"]require[/URL] [COLOR= #DD0000]urlDispatcher.class.php[/COLOR][COLOR= #007700];[/COLOR]
[URL="http://www.php.net/echo"]echo[/URL] UrlDispatcher[COLOR= #007700]::[/COLOR]getPart[COLOR= #007700]([/COLOR][COLOR= #0000BB]0[/COLOR][COLOR= #007700]);[/COLOR] [COLOR= #FF8000]//verkrijg het eerste deel dus /paginanaam/XX[/COLOR]
[URL="http://www.php.net/if"]if[/URL] [COLOR= #007700]([/COLOR]UrlDispatcher[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR]partExist[COLOR= #007700]([/COLOR][COLOR= #0000BB]0[/COLOR][COLOR= #007700]) ==[/COLOR] TRUE[COLOR= #007700]) {[/COLOR] [COLOR= #FF8000]//kijk of het eerste deel wel bestaat[/COLOR]
[URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"Je zit in topic ID: "[/COLOR] [COLOR= #007700].[/COLOR] UrlDispatcher[COLOR= #007700]::[/COLOR]getPart[COLOR= #007700]([/COLOR][COLOR= #0000BB]0[/COLOR][COLOR= #007700]);
}[/COLOR] [URL="http://www.php.net/else"]else[/URL] [COLOR= #007700]{[/COLOR]
[URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000]"Je hebt geen topic gekozen!"[/COLOR][COLOR= #007700];
}[/COLOR]
?>
!Raoul
