hm-hajjaji/route 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hm-hajjaji/route

最新稳定版本:v1.1

Composer 安装命令:

composer require hm-hajjaji/route

包简介

The route component defines the system route based on the php attribute

README 文档

README

The route component defines the system route based on the php attribute

Usage

Step 1: Installation

To install PHP Router, You can run the following command directly at your project path in your console:

composer require hm-hajjaji/route

Step 2: Example Usage

In your project's index.php file, create an instance of the HttpRoute class and pass the parameter controller_dir path to your project controllers

<?php
//index.php
use Route\Http\HttpRoute;
//get autoload of composer
require_once dirname(__DIR__)."/vendor/autoload.php";

//create object from class HttpRoute
(new HttpRoute("path controllers"))->resolve();

Create a controller and add Route attribute to the action index

<?php
namespace App\Controller;
use Route\Route;

class HomeController
{
    #[Route("/home","app_home")]
    public function index()
    {
        echo "welcome home";
    }
}

Step 3: Run

Run your own localhost server Example :

php -S localhost:8000

And finally go to url http://localhost:8000/home .

Prefix

Prefix action route in one controller

<?php
namespace App\Controller;
use Route\Route;

#[Route("/user")]
class HomeController
{
    #[Route("/home","app_home")]
    public function index()
    {
        echo "welcome home";
    }
    
    #[Route("/show","app_show")]
    public function show()
    {
        echo "welcome show";
    }
}

Passing parameters

You can pass multiple parameters for the routing

<?php
namespace App\Controller;
use Route\Route;

class HomeController
{
    #[Route("/home/{name}","app_home")]
    public function index($name)
    {
        echo "welcome $name";
    }
}

Now to access to action do the following:

go to url http://localhost:8000/home/name_value .

Output the : welcome name_value

Recuperate the path

You can recuperate the path from the route name using the path function:

<a href="<?=HttpRoute::path("app_home",['name' => 'Home'])?>">Home</a>

The path will be: http://localhost:8000/home/Home .

统计信息

  • 总下载量: 39
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-07-19