rgehan/router

一个非常简单的无依赖 PHP 路由器

1.0.4 2017-04-19 12:14 UTC

This package is not auto-updated.

Last update: 2024-09-29 03:47:04 UTC


README

这是一个非常简单的路由器,它没有任何外部依赖。它允许你定义路由,针对特定的动词,并将它们映射到控制器类中的方法。

我大致模仿了 Laravel 路由器。

安装

使用 Composer 简单地引入它

composer require rgehan/router

用法

以下代码应位于一个 PHP 文件中,所有请求都应重定向到该文件(例如使用 .htaccess 文件)。

<?php

// Requires the Composer autoloader, allowing us to use external modules
require_once(__DIR__ . "/../vendor/autoload.php");

use rgehan\RouterPHP\Router;

// Sets the namespace in which the Router will look for controllers
Router::setControllerNamespace('\\rgehan\\myProject\\controllers\\');

// Sets the variables that will be passed to all controllers methods
Router::setRoutesGlobalParameters(['global variable 1', 123, [1, 2, 3]]);

// Defines the routes
Router::get('/', 'HomeController@index');
Router::get('/home', 'HomeController@index');
Router::get('/articles', 'ArticlesController@index');
Router::get('/article', 'ArticlesController@get');

Router::post('/search/name', 'ArticlesController@search');

Router::delete('/article', 'ArticlesController@delete');

Router::update('/article', 'ArticlesController@delete');

// Dispatch the current request to the correct controller/method
Router::dispatch();

当前限制

与许多路由器不同,此路由器不允许你在 URL 中定义动态参数(例如,例如 /article/{id})。你仍然需要依赖于老式的 $_GET$_POST 来获取你的数据。

毕竟,它只是一个路由器,不是一个完整的框架 :)