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, (*1)
composer require hispanicode/route
In the downloaded files you will find a folder named example, from there you can see different routing tests., (*2)
Important. The .htaccess is required, in the example folder is the .htaccess, simply copy it to the root of your project., (*3)
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, (*4)
post($route, $ControllerAction, $regex = array()) - generate post route, (*5)
put($route, $ControllerAction, $regex = array()) - generate put route, (*6)
delete($route, $ControllerAction, $regex = array()) - generate delete route, (*7)
verb($verb = array(), $route, $ControllerAction, $regex = array()) - generate diferents requests to one route. Example: array("get", "post", "put", "delete"), (*8)
group(array $middleware, $group) - grouping routes in a middleware, (*9)
get_collection() - get array with routes collection with two possibles values 0 or 1, If 1 is found, then the route is valid, (*10)
get_controller() - get controller associated with the route, (*11)
base_url() - get base url, (*12)
request_uri() - get request uri, (*13)
is_ssl() - check if is ssl (https), (*14)
get_route() - get the current route, (*15)
request_method() - get the request method, (*16)
csrf_token($bool) - Create csrf_token session, (*17)
get_csrf_token() - get csrf_token, (*18)
url($route, $args = array()) - generate url from a route, (*19)