millennium/router

Millennium PHP Router

v2.0.1 2016-02-21 12:24 UTC

This package is not auto-updated.

Last update: 2024-12-21 19:05:18 UTC


README

PHP Router is totally inspired by Gatakka/PGF-Router and does not use regular expressions too.

Dependency Status Latest Stable Version Total Downloads Latest Unstable Version License

Requirements:

  • "symfony/yaml": "*"

Suggest

  • "php": ">=5.4.0"
  • "ext-mbstring": "*"

Features:

  • Integrate Memcache or other cache systems
  • Maybe some refactoring and optimizing

Installation and configuration:

Install with Composer, run:

composer require millennium/phpcache

Routers example

This is sample router configuration

path: /                         # require
action:                         # Some action, we use Namespace:Controller:action
methods: []                     # any valid HTTP methods combination (GET, POST, PUT, DELETE) **ONLY UPPER STRING**
requires: []                    # requires url parameters
defaults: []                    # defaults parameters
security: []                    # this is future, for now is only validation ip request (eg. allow only from ips array)
import: path                    # you can import other yaml routing files, path, security and methods will be overriding

File look like this

homepage:
    path: /
    action: Namespace:Controller:action
user_action:
    path: /user/:id/:action
    action: Namespace:Controller:action
    methods:
        - GET
    requires:
        id: digit
        action: [add, view, edit, delete]
admin_user:
    path: /admin
    import: ./config/routes_user_admin.yml
    security:
        - { ip: [127.0.0.1, ::1] }

Usage examples:

<?php

include_once '../vendor/autoload.php';

use Millennium\Router\Router;
use Millennium\Router\RouterCollection;

$routerCollection = new RouterCollection();
$collections = $routerCollection->collectRouters("router.yml");

$router = new Router();

try {
    $route = $router->findRoute('/', $collections);
} catch (\Exception $e) {
    trigger_error($e->getMessage(), E_USER_ERROR);
}

Server Configuration

Apache

You may need to add the following snippet in your Apache HTTP server virtual host configuration or .htaccess file.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [L]

Alternatively, if you’re lucky enough to be using a version of Apache greater than 2.2.15, then you can instead just use this one, single line:

FallbackResource /index.php

IIS

For IIS you will need to install URL Rewrite for IIS and then add the following rule to your web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
          <rule name="Toro" stopProcessing="true">
            <match url="^(.*)$" ignoreCase="false" />
              <conditions logicalGrouping="MatchAll">
                <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                <add input="{R:1}" pattern="^(index\.php)" ignoreCase="false" negate="true" />
              </conditions>
            <action type="Rewrite" url="/index.php/{R:1}" />
          </rule>
        </rewrite>
    </system.webServer>
</configuration>

Nginx

Under the server block of your virtual host configuration, you only need to add three lines.

location / {
  try_files $uri $uri/ /index.php?$args;
}

Contributions

Contributions to PHPRouter are welcome via pull requests.

License

PHPRouter was created by Zlatko Hristov and released under the MIT License.