Toro
Toro is a PHP router for developing RESTful web applications and APIs. It is
designed for minimalists who want to get work done., (*1)
Quick Links
Features
- RESTful routing using strings, regular expressions, and defined types
(
number
, string
, alpha
)
- Flexible error handling and callbacks via
ToroHook
- Intuitive and self-documented core (
Toro.php
)
- Tested with PHP 5.3 and above
"Hello, world"
The canonical "Hello, world" example:, (*2)
"HelloHandler",
));
```
## Routing Basics
Routing with Toro is simple:
```php
"SplashHandler",
"/catalog/page/:number" => "CatalogHandler",
"/product/:alpha" => "ProductHandler",
"/manufacturer/:string" => "ManufacturerHandler"
));
```
An application's route table is expressed as an associative array
(`route_pattern => handler`). This is closely modeled after
[Tornado](http://tornadoweb.org) (Python). Routes are not expressed as
anonymous functions to prevent unnecessary code duplication for RESTful
dispatching.
From the above example, route stubs, such as `:number`, `:string`, and
`:alpha` can be conveniently used instead of common regular expressions.
Of course, regular expressions are still welcome. The previous example could
also be expressed as:
```php
"SplashHandler",
"/catalog/page/([0-9]+)" => "CatalogHandler",
"/product/([a-zA-Z0-9-_]+)" => "ProductHandler",
"/manufacturer/([a-zA-Z]+)" => "ManufacturerHandler"
));
```
Pattern matches are passed in order as arguments to the handler's request
method. In the case of `ProductHandler` above:
```php
installer.php
$ less installer.php
$ # When you're certain it's safe...
$ php installer.php
```
Create a `composer.json` file in your project root:
```js
{
"require": {
"torophp/torophp": "dev-master"
}
}
```
Install via composer:
```sh
$ php composer.phar install
```
### Server Configuration
#### Apache
You may need to add the following snippet in your Apache HTTP server virtual host configuration or **.htaccess** file.
```apacheconf
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:
```apacheconf
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
<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., (*3)
location / {
try_files $uri $uri/ /index.php?$args;
}
Contributions
Contributions to Toro are welcome via pull requests., (*4)
License
ToroPHP was created by Kunal Anand and released under
the MIT License., (*5)