Skip to content

Instantly share code, notes, and snippets.

@segg21
Last active August 24, 2018 08:01
Show Gist options
  • Save segg21/ae1c56af0999be898dee1ec5ced6318c to your computer and use it in GitHub Desktop.
Save segg21/ae1c56af0999be898dee1ec5ced6318c to your computer and use it in GitHub Desktop.
Simple routing php
<?php
/*
\| Route/PHPRouting101
\| @author => LegitSoulja
\| @copyright => LegitSoulja
\| @license => Apache
\| @source => https://gist.github.com/LegitSoulja/ae1c56af0999be898dee1ec5ced6318c
\| @source => https://github.com/LegitSoulja/PHPRouting101
*/
/*
\| Route/PHPRouting101
\| @author => LegitSoulja
\| @copyright => LegitSoulja
\| @license => Apache
\| @source => https://gist.github.com/LegitSoulja/ae1c56af0999be898dee1ec5ced6318c
\| @source => https://github.com/LegitSoulja/PHPRouting101
*/
class Route {
static _arrToObj(a, b) {
for (var i in b) {
var o = b[i].split('=');
a[o[0]] = o[1];
}
}
static get() {
// document.location was copied/cloned because I didn't want to manipulate any window changes literally
var loc = JSON.parse(JSON.stringify(document.location));
if (loc.pathname) {
if (!loc.search) {
var _try = loc.pathname.indexOf('&');
if (_try > -1) {
loc.search = loc.pathname.slice(_try);
loc.pathname = loc.pathname.slice(0, _try);
}
}
if (loc.pathname[loc.pathname.length - 1] === "/")
loc.pathname = loc.pathname.slice(0, -1);
var args = {};
var path = [];
if (loc.search) {
this._arrToObj(args, loc.search.slice(1).split('&'));
}
var res = loc.pathname.slice(1).split('/');
if (res.length > 0 && res[0] !== "") {
path = loc.pathname.slice(1).split('/');
}
return {
path: path,
args: args
}
}
return {
path: [],
args: new Proxy({}, {
get: function(target) {
return target;
}
}),
}
}
}
@segg21
Copy link
Author

segg21 commented Nov 19, 2017

PHPRouting101

PHP Routing done with skill, and simplistic perfection.

Compatible URI’s
  • example.com/some/route/?a=b&c=d
  • example.com/some/route?a=b&c=d
  • example.com/some/route&a=b&c=d
Installation
  • Install Route.php
  • Include "Route.php"
Usage
// Note* This is an object NOT an array, however you can change it to an array in the parenthesis if needed.
(object) $route = Route::get();
Properties
  • path [Array] (A path of the uri, as of compatible uri's it would be [some, route])
  • args [Array(Associative) (query/get information at the end of the uri)
Tips/Warnings
  • Try not to have any query values containing an equal sign (=), as it can cause false data readings.
// uri = example.com/some/route/?a=b&c=d

print_r(Route::get());
/*
stdClass Object
(
    [path] => Array
        (
            [0] => some
            [1] => route
        )

    [args] => Array
        (
            [a] => b
            [c] => d
        )

)
   
*/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment