openclerk/routing 问题修复 & 功能扩展

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

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

openclerk/routing

最新稳定版本:0.1.0

Composer 安装命令:

composer require openclerk/routing

包简介

A library for simple routing in PHP

README 文档

README

A library for simple routing in PHP.

Using

This project uses openclerk/config for config management.

First configure the component with site-specific values:

Openclerk\Config::merge(array(
  "absolute_url" => "http://localhost/path/",
));

Add a simple router file:

<?php
// router.php

require(__DIR__ . "/../inc/global.php");
$path = require_get("path", "security/login/password");

try {
  \Openclerk\Router::process($path);
} catch (\Openclerk\RouterException $e) {
  header("HTTP/1.0 404 Not Found");
  echo htmlspecialchars($e->getMessage());
}
?>

Add a .htaccess that translates paths to this router:

RewriteEngine on

# Forbid access to any child PHP scripts
RewriteRule ^([^\.]+)/([^\.]+).php$   -                   [F]

RewriteRule ^([^\.]+)$                router.php?path=$1  [L,QSA]

Define site routes:

// set up routes
\Openclerk\Router::addRoutes(array(
  "security/login/password" => "security/login.php?type=password",
  "security/login/:key" => "security/login-:key.php?type=:key",
  // by default any unmatched routes will require <module>.php
));

Now you can use url_for() and absolute_url_for():

<a href="<?php echo htmlspecialchars(url_for('security/login/password')); ?>">Login with password</a>

Renderable objects

You can also pass along an object with a render($args) method, which will be called instead:

class MyApi {
  function render($args) {
    // $args = array('code' => ...)
  }
}

\Openclerk\Router::addRoutes(array(
  "api/currency/:code" => new MyApi(),
));

Tests

composer update --dev
vendor/bin/phpunit

TODO

  1. Actual documentation
  2. Tests
  3. Publish on Packagist

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2014-12-27