一个用于定义路由的PHP模型,具有多种可能性,例如Laravel的路由,但无需依赖任何东西,您可以轻松地将此模型适应到您的MVC系统中。

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

This package is not auto-updated.

Last update: 2024-09-14 19:31:10 UTC


README

route

一个用于定义路由的PHP模型,具有多种可能性,例如Laravel的路由,但无需依赖任何东西,您可以轻松地将此模型适应到您的MVC系统中。

使用composer安装

	composer require hispanicode/route

在下载的文件中,您将找到一个名为example的文件夹,在那里您可以查看不同的路由测试。

重要。需要 .htaccess 文件,示例文件夹中包含 .htaccess 文件,只需将其复制到您项目的根目录。

routes.php 示例

<?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 示例

<?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"];
}

}

静态方法

get($route, $ControllerAction, $regex = array()) - 生成get路由

post($route, $ControllerAction, $regex = array()) - 生成post路由

put($route, $ControllerAction, $regex = array()) - 生成put路由

delete($route, $ControllerAction, $regex = array()) - 生成delete路由

verb($verb = array(), $route, $ControllerAction, $regex = array()) - 生成对同一路由的不同请求。例如:array("get", "post", "put", "delete")

group(array $middleware, $group) - 将路由分组到中间件中

get_collection() - 获取包含路由集合的数组,具有两个可能的值 0 或 1,如果值为 1,则表示该路由有效

get_controller() - 获取与路由关联的控制器

base_url() - 获取基础URL

request_uri() - 获取请求URI

is_ssl() - 检查是否是ssl(https)

get_route() - 获取当前路由

request_method() - 获取请求方法

csrf_token($bool) - 创建csrf_token会话

get_csrf_token() - 获取csrf_token

url($route, $args = array()) - 从路由生成URL