Download the PHP package lucatume/klein52 without Composer
On this page you can find all versions of the php package lucatume/klein52. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lucatume/klein52
More information about lucatume/klein52
Files in lucatume/klein52
Package klein52
Short Description A lightning fast router for PHP 5.2 (fork of chriso/klein.php)
License MIT
Homepage https://github.com/lucatume/klein.php
Informations about the package klein52
klein.php is a lightning fast router for PHP 5.3+; klein52 is its port to PHP 5.2 compatible code.
This version is a fork of klein.php v. 1.2.0
that has been modified to run in PHP 5.2
; this is not up to date with klein.php latest version.
All kudos to the original author.
- Flexible regular expression routing (inspired by Sinatra)
- A set of boilerplate methods for rapidly building web apps
- Almost no overhead => 2500+ requests/second
Getting started
- PHP 5.2.17 is required
- Setup URL rewriting so that all requests are handled by index.php
- Use
<?php require 'klein_load.php';
- use the
klein_load()
function to load a PHP 5.2+ compatible, not namespaced version ofklein
(as the original) or useklein_wp_load()
to load aklein_
prefixed, PHP 5.2+ and WordPress compatible version of the library - (Optional) Throw in some APC for good measure; the version of the library loaded with
klein_wp_load()
will rely on WordPress caching functions
Example
In all the examples below the the not prefixed functions and class names from the not version of the library loaded with klein_load()
are shown; add a klein_
prefix to each function and class name if the library was loaded using klein_wp_load
.
Only exception to the above rule: the inNamespace()
function of the default library transalates to the klein_with
function in the WordPress compatible version of the library.
Example 1.1 - Respond to all requests (PHP 5.3 syntax)
Example 1.2 - Respond to all requests (PHP 5.2 syntax)
Note: in all of the examples below I will use PHP 5.3+ compatible code for brevity; to make any example work with PHP 5.2 compatible syntax replace any closure with a named function (see Example 1.2).
Example 2 - Named parameters
Example 3 - So RESTful
Example 4 - Sending objects / files
Example 5 - All together
Route namespaces
Lazy services
Services can be stored lazily, meaning that they are only instantiated on first use.
Validators
To add a custom validator use addValidator($method, $callback)
You can validate parameters using is<$method>()
or not<$method>()
, e.g.
Validation methods are chainable, and a custom exception message can be specified for if/when validation fails
Routing
[ match_type : param_name ]
Some examples
* // Match all request URIs
[i] // Match an integer
[i:id] // Match an integer as 'id'
[a:action] // Match alphanumeric characters as 'action'
[h:key] // Match hexadecimal characters as 'key'
[:action] // Match anything up to the next / or end of the URI as 'action'
[create|edit:action] // Match either 'create' or 'edit' as 'action'
[*] // Catch all (lazy)
[*:trailing] // Catch all as 'trailing' (lazy)
[**:trailing] // Catch all (possessive - will match the rest of the URI)
.[:format]? // Match an optional parameter 'format' - a / or . before the block is also optional
Some more complicated examples
/posts/[*:title][i:id] // Matches "/posts/this-is-a-title-123"
/output.[xml|json:format]? // Matches "/output", "output.xml", "output.json"
/[:controller]?/[:action]? // Matches the typical /controller/action format
Note - all routes that match the request URI are called - this allows you to incorporate complex conditional logic such as user authentication or view layouts. e.g. as a basic example, the following code will wrap other routes with a header and footer
Routes automatically match the entire request URI. If you need to match
only a part of the request URI or use a custom regular expression, use the @
operator. If you need to
negate a route, use the !
operator
Views
You can send properties or helpers to the view by assigning them
to the $response
object, or by using the second arg of $response->render()
myview.phtml
Views are compiled and run in the scope of $response
so all response methods can be accessed with $this
-## API
More information
See the wiki for more information
Contributors
License
(MIT License)
Copyright (c) 2010 Chris O'Hara [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.