sagittariusx/beluga.routing 问题修复 & 功能扩展

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

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

sagittariusx/beluga.routing

Composer 安装命令:

composer require sagittariusx/beluga.routing

包简介

Simple URL routing library.

README 文档

README

Simple URL routing library.

Installation

Install it via composer!

composer require sagittariusx/beluga.routing

or include it inside you're composer.json

{
   "require": {
      "php": ">=7.0",
      "sagittariusx/beluga.": "^0.1.1"
   }
}

Preparing the web server

For Router usage you need to tell the web server, that it should rewrite requests to not existing URL paths to the handling PHP script.

You can do it by 2 different ways:

  • Passing the not existing URL path as GET variable with specific name, to the script
  • Passing the not existing URL path VIA $_SERVER[ 'REQUEST_URI' ] (best choice)

Apache web server

For apache its really simple to handle the rewrites.

Create an file with the name .htaccess and put it to the folder where the rewriting should work.

But remember!

.htaccess (distributed configuration files) should only be used if you do'nt have access to the server configuration files.

.htaccess usage comes with some overhead which can be avoided.

But if you admit an server, there is no need to shown you more. You have to know it :-)

The contents of this file depends to the Router type that should be used.

As GET variable (RouterType::REWRITE_TO_GET)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?theURL=$1 [QSA,L]

The first line enables the rewrite engine. Second line declares the condition that matches all not existing file calls and the third line matches all not existing directory calls.

The last line rewrites the matching calls to not existing files and directories to index.php and passes the called, not existing URL path to the theURL GET variable

As 'REQUEST_URI' value (RouterType::REWRITE_TO_REQUEST_URI)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

The first line enables the rewrite engine. Second line declares the condition that matches all not existing file calls and the third line matches all not existing directory calls.

The last line rewrites the matching calls to not existing files and directories to index.php and the called. not existing URL path is passed to $_SERVER[ 'REQUEST_URI' ]

On errors

If the .htaccess usage triggers some server errors (e.g. error 50* or something else) you have to check:

  • if apache is enabled to use .htaccess files
  • if apache is enabled to use rewriting by .htaccess files (Rights for .htaccess stuff)
  • if apache is enabled to use the mod_rewrite extension

The first 2 things can be handled by ensure the AllowOverwrite directive. For details see: Apache HTTP Server Tutorial: .htaccess files

AllowOverride FileInfo

To check if mod rewrite is enable call this inside you're console

sudo a2enmod rewrite

If already enabled it outputs: Module rewrite already enabled

If not enabled, the rewrite module gets enabled.

If you not have access to call a console at the server contact the admin or provider and ask him if mod_rewrite is enabled for you. If not please him to enable the mod_rewrite usage via .htaccess

NGINX web server

This is a big TODO! :-)

but i think if you use:

try_files $uri $uri/ /index.php?$args;

…it should work to get the called URL path via $_SERVER[ 'REQUEST_URI' ] inside index.php

Usage

// Include the autoloader, created by composer
require __DIR__ . '/vendor/autoload.php';

use Beluga\Routing\Router;
use Beluga\Routing\RouterType;

// Init the router
$router = new Router(
   // The type of the router
   RouterType::REWRITE_TO_REQUEST_URI
);

// Adds an dynamic regex URI path route
$router->addRoute(
   '~^/foo/(\d+)/bar/?$~',
   function ( array $matches )
   {
      // If you need access to current Router instance can get it by Router::GetInstance()
      echo 'ID: ', $matches[ 1 ], ' OK :-)';
      exit;
   }
);

// Adds an static URI path route
$router->addRoute(
   '~^/baz/?$~',
   function ()
   {
      // If you need access to current Router instance can get it by Router::GetInstance()
      echo 'The BAZ is called!';
      exit;
   }
);

After defining you're routes you only should call execute() and the routes will be executed.

if ( ! $router->execute() )
{
   // Showing 404 error because no router matches the defined request URI path.
}

sagittariusx/beluga.routing 适用场景与选型建议

sagittariusx/beluga.routing 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 08 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 sagittariusx/beluga.routing 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sagittariusx/beluga.routing 我们能提供哪些服务?
定制开发 / 二次开发

基于 sagittariusx/beluga.routing 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: LGPLv3
  • 更新时间: 2016-08-08