Php model to define routes with a wide range of possibilities, like routing of laravel, but without relying on anyone, you can adapt this model to your model view controller system very easily.

dev-master 2016-07-02 18:52 UTC

This package is not auto-updated.

Last update: 2024-04-13 17:19:03 UTC


README

route

Php model to define routes with a wide range of possibilities, like routing of laravel, but without relying on anyone, you can adapt this model to your model view controller system very easily.

Install with composer

	composer require hispanicode/route

In the downloaded files you will find a folder named example, from there you can see different routing tests.

Important. The .htaccess is required, in the example folder is the .htaccess, simply copy it to the root of your project.

routes.php example

<?php 
session_start();
require "../src/Route.php";
require "Controllers/TestController.php";
use Hispanic\Route;

Route::get("", "TestController@home");
Route::get("home/home", "TestController@home");
Route::get("home/get", "TestController@get");
Route::get("home/string", function(){
	return "Hello World";
});
//Route::post(...)
//Route::put(...) include in the form the next field <input type="hidden" name="_method" value="put" />
//Route::delete(...) include in the form the next field <input type="hidden" name="_method" value="delete" />

//Route::verb(array("get", "post", "put", "delete"), ...)
Route::verb(array("get"), "home/login", "TestController@login");
Route::verb(array("post"), "home/check_login", "TestController@check_login");

//Route::group(array("auth"), ... auth is a var session, for example: $_SESSION["auth"] = true, if the sessions exists these routes are availables
Route::group(array("auth"), function(){
	Route::get("home/dashboard", "TestController@dashboard");
});

//Pass arguments in the routes and filter with regular expressions
$filter_arguments = array("arg1" => "/^[0-9]+$/", "arg2" => "/^[a-z\s]+$/i");
Route::get("home/arguments/{arg1}/{arg2}", "TestController@arguments", $filter_arguments);
//Pass optional argument
Route::get("home/optional_argument/{arg=hello world}", "TestController@optional_argument");

/* csrf token securiry */
Route::csrf_token(true);
//if the route not exists
if (array_search(1, Route::get_collection()) === false) {
	header("HTTP/1.0 404 Not Found", true, 404);
	echo "<h2>ERROR 404</h2>";
	exit;
}

TestController.php example

<?php

use Hispanic\Route;

class TestController{

public function home()
{
	include "Views/home/home.php";
}

public function get()
{
	return "The value is: " . $_GET["name"];
}

public function login()
{
	//Example if you like show a view, create your custom model view, the include is not the best solution
	include "Views/home/login.php";
}

public function check_login()
{
	if (isset($_POST["name"]) && isset($_POST["password"])) {
		if ($_POST["name"] == "demo" && $_POST["password"] == "demo") {
			$_SESSION["auth"] = true;
			$_SESSION["user_name"] = $_POST["name"];
			header("location: " . Route::url("home/dashboard"));
			exit;
		}
	}
	header("location:" . Route::url("home/login"));
	exit;
}

public function dashboard()
{
	return "Welcome " . $_SESSION["user_name"];
}

public function arguments()
{
	return "Arg1 = " . $_GET["arg1"] . "| Arg2 = " . urldecode($_GET["arg2"]);
}

public function optional_argument()
{
	return "Arg = " . $_GET["arg"];
}

}

public static methods

get($route, $ControllerAction, $regex = array()) - generate get route

post($route, $ControllerAction, $regex = array()) - generate post route

put($route, $ControllerAction, $regex = array()) - generate put route

delete($route, $ControllerAction, $regex = array()) - generate delete route

verb($verb = array(), $route, $ControllerAction, $regex = array()) - generate diferents requests to one route. Example: array("get", "post", "put", "delete")

group(array $middleware, $group) - grouping routes in a middleware

get_collection() - get array with routes collection with two possibles values 0 or 1, If 1 is found, then the route is valid

get_controller() - get controller associated with the route

base_url() - get base url

request_uri() - get request uri

is_ssl() - check if is ssl (https)

get_route() - get the current route

request_method() - get the request method

csrf_token($bool) - Create csrf_token session

get_csrf_token() - get csrf_token

url($route, $args = array()) - generate url from a route