shanta280/easy_php_router

轻量级、简单、单类、快速 PHP 路由器(路由系统)

1.1.5 2018-09-05 19:21 UTC

This package is not auto-updated.

Last update: 2024-09-23 18:35:49 UTC


README

轻量级、简单、快速 PHP 路由器(PHP 路由系统)(单类)

单类路由,使用简单,易于集成,易于实现,易于开始使用,易于修改

如何安装

    composer require shanta280/easy_php_router

基本示例

<?php

require_once __DIR__.'/../vendor/autoload.php';
// or 
// require_once path_to_file/EasyRouter.php


$r = new Shantanu\EasyRouter();

$r->get("/", function() {
    echo "Home page";
});

$r->get("/about/{name}", function($name="") {
    echo "About {$name}";
});

// good this page will be shown 
// when url does not maych any thing
// good for showing 404
$r->get("/{any}", function($any='') {
    echo $any;
});

$r->get("/contact", function() {
    echo "Contact Page";
});

$r->get("/services", function() {
    echo "Services Page";
});

$r->any("/anymethod", function() {
    // we can call this route with any method
});

// to set custom 404 page
$r->set404(function() {
    die("Custom 404 page");
});

$r->run();// this line is important, it starts the routing process

支持的方法

$r->get("path", "callback")
$r->post("path", "callback")
$r->put("path", "callback")
$r->delete("path", "callback")
// another special method
$r->route("method", "path", "callback")
$r->any("path", "callback")
$r->set404("callback")

支持的回调方法

// Closure method
$r->get("/", function() {
    // do something here
});
 
// namespace method
// this will call the index function of MainController
$r->get("/", "\Namespace\Controller\MainController::index");

// class method
// this will call the about method of MyClass
$r->get("/about", ["MyClass", "about"]);

// we can also use someting like below
$r->route("GET", "/someting", ["\Namespace\Controller\SomeController", "some_method"]);

更多信息

在回调中,我们可以传递命名空间\类,仅类,闭包,更多,如果你扩展它。