danidoble/routing

以symfony-routing为起点的一种简单方法

安装: 57

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:插件

v1.0.3 2023-02-17 19:10 UTC

This package is auto-updated.

Last update: 2024-09-17 23:02:15 UTC


README

以symfony/routing为起点的一种简单方法

版本^v1.0需要PHP 8.1

composer require danidoble/routing

Debug::enable(); 这仅用于调试模式,将其更改为自定义页面,请检查symfony/error-handler

创建一个名为 .env 的文件,内容如下

APP_URL="https:///"
APP_DEBUG=true
APP_VIEW_DIR="views"

将localhost更改为您的网站URL

use Symfony\Component\ErrorHandler\Debug;
use Danidoble\Routing\Route;
use Danidoble\Routing\Testing;

include __DIR__ . "/vendor/autoload.php";

// This is only for debug mode, change it for a custom page, check [symfony/error-handler](https://github.com/symfony/error-handler)
Debug::enable();

$base_path = __DIR__ . DIRECTORY_SEPARATOR; // base path of your project
$base_view_path = env('APP_VIEW_DIR','views'); // route of your directory of views
\Danidoble\Routing\View::$BASE_VIEW_PATH = $base_path . env('APP_VIEW_DIR'); // example: /home/your/project/views
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__");
$dotenv->load();

实例化 \Danidoble\Routing\Route; 的Route

$routes = new Route();

//add routes
$routes->add('/', [Testing::class, 'index'])->setMethods(['GET','POST']); //only get and post allowed
$routes->add('/help', [Testing::class, 'index'])->setMethods('GET'); //only get allowed
$routes->add('/danidoble', [Testing::class, 'index', 'a']); // all methods allowed

// dispatch 
$routes->dispatch();

创建路由示例

$path = "/lorem";//route slug
$controller = \Danidoble\Routing\Testing::class; // class/controller
$method = "index"; // method to execute
$route_name = "example_route"; // name of route

$routes->add($path,[$controller,$method,$route_name]);

在您的控制器中

class Testing extends \Danidoble\Routing\Controller
{
    public function index()
    {
        //dump($this->getParent());
        //dump($this->param('demo'));
        dump($this->getParams());
    }
}

如果您想重写 __construct(),请确保应用一些如下代码

public function __construct(RequestContext $_context, RouteCollection $_routes, UrlMatcher $_matcher, Route $_parent)
{
    parent::__construct($_context, $_routes, $_matcher, $_parent);
    // ... your code here
}