[PHP] [OOP] URL class voor betere URLs

Gestart doorOne2xs15 reacties6 weergaven

16 berichten · pagina 1 van 2

One2xs

Bot · Topicstarter

Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
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:
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]

?>
Zet dit in je .htaccess:

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]

?>
Veel plezier ermee, laat wel mijn copyright staan svp biggrin!

Raoul
0
Berichten
6.485
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Is gewoon mod rwrite niet sneller?
0
One2xs

Bot · Topicstarter

Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Rogier schreef:
Is gewoon mod rwrite niet sneller?
Misschien wel, maar dit gaat via PHP en is dus ook sneller omdat je niet bij iedere pagina moet rewriten
0
Rik

Lid

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Rogier schreef:
Is gewoon mod rwrite niet sneller?
De multiviews manier is makkelijker omdat je dan alle urls in één keer rewrite als het ware.
0
One2xs

Bot · Topicstarter

Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Rik schreef:
Eerdere citaten tonen
Rogier schreef:
Is gewoon mod rwrite niet sneller?
De multiviews manier is makkelijker omdat je dan alle urls in één keer rewrite als het ware.
Met multiviews haal je gewoon de extensie weg, maar zonder Multiviews zou je bv index.php/bla/bla hebben en dat staat ook niet echt mooi
0
Berichten
6.485
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Rik schreef:
Eerdere citaten tonen
Rogier schreef:
Is gewoon mod rwrite niet sneller?
De multiviews manier is makkelijker omdat je dan alle urls in één keer rewrite als het ware.
Je kan toch ook met varable werken? Ik heb meestal aan 4 rijen genoeg. Dat scheelt zo een hele class.

De class is trouwens wel heel netjes!
0
One2xs

Bot · Topicstarter

Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Rogier schreef:
Eerdere citaten tonen
Rik schreef:
Eerdere citaten tonen
Rogier schreef:
Is gewoon mod rwrite niet sneller?
De multiviews manier is makkelijker omdat je dan alle urls in één keer rewrite als het ware.
De class is trouwens wel heel netjes!
Bedankt smile
0
Berichten
20.364
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Params werken makkelijker.

if(isset($param[1]))
0
One2xs

Bot · Topicstarter

Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Shahiem schreef:
Params werken makkelijker.
if(isset($param[1]))


Nee, dit is via OOP en als je een systeem hebt via OOP is het veel beter om een class te gebruiken.
0
Berichten
10.421
Reputatie
4
Punten
0,00
Lid sinds
Laatst gezien
- Hij stript GET-parameters overigens niet. Soms is dat wel wenselijk... smile
Voor bijvoorbeeld mijn multiviews-implementatie van mijn CMS doe ik dat wel. Enkele modules ondersteunen daar namelijk de xmlHttp-parameter, die de output in JSON notatie teruggeeft.

- Ik meen dat de array anders kan zijn bij het gebruik van een trailing slash. Ik ga dit anders eens een dezer dagen even uittesten zodra ik thuis ben.

Verder is het een tof script.
0
One2xs

Bot · Topicstarter

Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Ik denk dat ik best rtrim() er eens over gooi, over die request uri
0
Berichten
10.421
Reputatie
4
Punten
0,00
Lid sinds
Laatst gezien
dat deed ik geloof ik ook. Ik zal snel eens kijken hoe ik dit geïmplementeerd had.
0
Janm

Lid

Berichten
6.170
Reputatie
1
Punten
0,00
Lid sinds
Laatst gezien
Moet dit niet in tutorials?
0
One2xs

Bot · Topicstarter

Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Janm schreef:
Moet dit niet in tutorials?
Nee, ik geef gewoon een script vrij zonder echt de class uit te leggen.
0
Berichten
10.421
Reputatie
4
Punten
0,00
Lid sinds
Laatst gezien
[COLOR= #0000BB]<?php
$path[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/substr"]substr[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_SERVER[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]REQUEST_URI[/COLOR][COLOR= #007700]],[/COLOR] [URL="http://www.php.net/strlen"]strlen[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/dirname"]dirname[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_SERVER[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]SCRIPT_NAME[/COLOR][COLOR= #007700]])));[/COLOR]
[COLOR= #0000BB]$param[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/explode"]explode[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]/[/COLOR][COLOR= #007700],[/COLOR] [URL="http://www.php.net/trim"]trim[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$path[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]/[/COLOR][COLOR= #007700]));[/COLOR]

[COLOR= #0000BB]$elem[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array_pop"]array_pop[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$param[/COLOR][COLOR= #007700]);[/COLOR]
[COLOR= #0000BB]$pos[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/strpos"]strpos[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$elem[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]?[/COLOR][COLOR= #007700]);[/COLOR]
[URL="http://www.php.net/if"]if[/URL][COLOR= #007700](![/COLOR][COLOR= #0000BB]$pos[/COLOR][COLOR= #007700]){[/COLOR]
    [COLOR= #FF8000]//array seems good ? element cant be found en we pullem back[/COLOR]
    [COLOR= #0000BB]$param[/COLOR][COLOR= #007700][] =[/COLOR] [COLOR= #0000BB]$elem[/COLOR][COLOR= #007700];
}[/COLOR][URL="http://www.php.net/else"]else[/URL] [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$pos[/COLOR] [COLOR= #007700]>[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700]){[/COLOR]
    [COLOR= #FF8000]//we found something for the question mark. Put off the first piece.[/COLOR]
    [COLOR= #0000BB]$param[/COLOR][COLOR= #007700][] =[/COLOR] [URL="http://www.php.net/substr"]substr[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$elem[/COLOR][COLOR= #007700],[/COLOR][COLOR= #0000BB]0[/COLOR][COLOR= #007700],[/COLOR][COLOR= #0000BB]$pos[/COLOR][COLOR= #007700]);
}[/COLOR]
?>


Zo werkt mijn functie... thumbsup
0

Jouw reactie

Deel je idee, stel een vraag of help een ander verder.

Editor laden…

Praat mee met de One2xs-community.

Inloggen om te reageren