1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219:
<?php
/**
* Created by PhpStorm.
* User: samtax
* Date: 08/07/2018
* Time: 7:47 AM
*/
/**
@see https://github.com/nezamy/route
How it works
Routing is done by matching a URL pattern with a callback function.
Route for index.php, might be
$route->any('/', function() {
echo 'Hello World';
});
$route->any('/about', function() {
echo 'About';
});
$route->get(['/', 'index', 'home'], function() {
// Will match 3 pages with one function
});
=========== For “unlimited” optional parameters, you can do this: ================
// This example will match anything after blog/ - unlimited arguments
$route->get('/blog/*', function() {
// [$this] instanceof ArrayObject so you can get all args by getArrayCopy()
pre($this->getArrayCopy());
pre($this[1]);
pre($this[2]);
});
====================================================================================================== c
function pages() {
echo 'Page Content';
}
$route->get('/', 'pages');
================================================================================================================================
class Home
{
function pages() {
echo 'Home page Content';
}
}
$route->get('/', ['Home', 'pages']);
// OR
$home = new Home;
$route->get('/', [$Home, 'pages']);
// OR
$route->get('/', 'Home@pages');
================================================================================================================================
$route->any('/', function() {
// Any method requests
});
$route->get('/', function() {
// Only GET requests
});
$route->post('/', function() {
// Only POST requests
});
$route->put('/', function() {
// Only PUT requests
});
$route->patch('/', function() {
// Only PATCH requests
});
$route->delete('/', function() {
// Only DELETE requests
});
// You can use multiple methods. Just add _ between method names
$route->get_post('/', function() {
// Only GET and POST requests
});
============== Parameters ============
/ This example will match any page name
$route->get('/?', function($page) {
echo "you are in $page";
});
// This example will match anything after post/ - limited to 1 argument
$route->get('/post/?', function($id) {
// Will match anything like post/hello or post/5 ...
// But not match /post/5/title
echo "post id $id";
});
// more than parameters
$route->get('/post/?/?', function($id, $title) {
echo "post id $id and title $title";
});
============= Named Parameters =========
$route->get('/{username}/{page}', function($username, $page) {
echo "Username $username and Page $page <br>";
// OR
echo "Username {$this['username']} and Page {$this['page']}";
});
============= Named Parameters ===========
*/
/************************************************
* Route Config
************************************************/
require PATH_LIBRARY ."route/RouteSystem.php";
$app = RouteApp::instance();
$app->request = RouteRequest::instance();
$app->route = RouteSystem::instance($app->request);
$route = $app->route;
Config1::onRoute(exRoute1::getRoute());
/************************************************
* Route Init/Include
************************************************/
$FORM_ACTION_SHOULD_REDIRECT = true;
//require PATH_APP."route.php";
function api_and_form_default_route(){
global $route;
$route->any('form/*', function (){
global $FORM_ACTION_SHOULD_REDIRECT;
Session1::set('old', $_REQUEST);
echo json_encode( \ServerRequest1::callFunction(urldecode(Url1::getPageName()), ',', false) );
if(!$FORM_ACTION_SHOULD_REDIRECT) return $FORM_ACTION_SHOULD_REDIRECT = true;
else return Url1::redirect(Url1::backUrl());
});
$route->any(['api/*', 'restapi/*'], function (){ echo json_encode(\Api1::callFunction(urldecode(Url1::getPageName()))); });
}
api_and_form_default_route();
/**
* @param $name
* @param $actionFunction
*/
function makeRoute($name, $actionFunction) { global $route; $route->any($name, $actionFunction); }
/**
* @param string $onLoginFound_redirectTo
* @param array $errorMessage
*/
function make_default_route($onLoginFound_redirectTo = '/', $errorMessage = ['Welcome Back', 'You have Logged In Already, Please Logout out first and try again', 'error']) { //routes()['dashboard']
global $route;
$route->view('/forgot_password', 'pages.auth.forgot_password');
$route->view('/reset_password', 'pages.auth.reset_password');
$route->any('/register', function() use ($onLoginFound_redirectTo, $errorMessage){ if(User::isLoginExist()){ Url1::redirect(url($onLoginFound_redirectTo), $errorMessage); } else { echo view('pages.auth.register'); } });
$route->any('/login', function() use ($onLoginFound_redirectTo, $errorMessage){ if(User::isLoginExist()){ Url1::redirect(url($onLoginFound_redirectTo), $errorMessage); } else { echo view('pages.auth.login'); } });
$route->any('/logout', function() { return User::logout(); });
$route->get('/delete_account', function() { (User::getLogin(false))->delete(); });
}
$route->end();
// $route->end('/error404','pages.error404', function($error404PageName, $error404ViewName){
// if(!view_exists($error404ViewName)) return false;
// view($error404ViewName); return true;
// });