klein.php是 PHP 5.3+ 的快速且靈活的路由器
php composer.phar require klein/klein
require 'vendor/autoload.php';
Hello World - 強制性的 hello world 範例
<?php
require_once __DIR__ . ' /vendor/autoload.php ' ;
$ klein = new Klein Klein ();
$ klein -> respond ( ' GET ' , ' /hello-world ' , function () {
return ' Hello World! ' ;
});
$ klein -> dispatch ();
範例 1 - 回應所有請求
$ klein -> respond ( function () {
return ' All the things ' ;
});
範例 2 - 命名參數
$ klein -> respond ( ' /[:name] ' , function ( $ request ) {
return ' Hello ' . $ request -> name ;
});
範例 3 - 如此寧靜
$ klein -> respond ( ' GET ' , ' /posts ' , $ callback );
$ klein -> respond ( ' POST ' , ' /posts ' , $ callback );
$ klein -> respond ( ' PUT ' , ' /posts/[i:id] ' , $ callback );
$ klein -> respond ( ' DELETE ' , ' /posts/[i:id] ' , $ callback );
$ klein -> respond ( ' OPTIONS ' , null , $ callback );
// To match multiple request methods:
$ klein -> respond ( array ( ' POST ' , ' GET ' ), $ route , $ callback );
// Or you might want to handle the requests in the same place
$ klein -> respond ( ' /posts/[create|edit:action]?/[i:id]? ' , function ( $ request , $ response ) {
switch ( $ request -> action ) {
//
}
});
範例 4 - 發送物件/文件
$ klein ->respond( function ( $ request , $ response , $ service ) {
$ service -> xml = function ( $ object ) {
// Custom xml output function
}
$ service -> csv = function ( $ object ) {
// Custom csv output function
}
});
$ klein -> respond ( ' /report.[xml|csv|json:format]? ' , function ( $ request , $ response , $ service ) {
// Get the format or fallback to JSON as the default
$ send = $ request -> param ( ' format ' , ' json ' );
$ response -> $ send ( $ report );
});
$ klein -> respond ( ' /report/latest ' , function ( $ request , $ response , $ service ) {
$ response -> file ( ' /tmp/cached_report.zip ' );
});
範例 5 - 全部在一起
$ klein -> respond ( function ( $ request , $ response , $ service , $ app ) use ( $ klein ) {
// Handle exceptions => flash the message and redirect to the referrer
$ klein -> onError ( function ( $ klein , $ err_msg ) {
$ klein -> service ()-> flash ( $ err_msg );
$ klein -> service ()-> back ();
});
// The fourth parameter can be used to share scope and global objects
$ app -> db = new PDO (...);
// $app also can store lazy services, e.g. if you don't want to
// instantiate a database connection on every response
$ app -> register ( ' db ' , function () {
return new PDO (...);
});
});
$ klein -> respond ( ' POST ' , ' /users/[i:id]/edit ' , function ( $ request , $ response , $ service , $ app ) {
// Quickly validate input parameters
$ service -> validateParam ( ' username ' , ' Please enter a valid username ' )-> isLen ( 5 , 64 )-> isChars ( ' a-zA-Z0-9- ' );
$ service -> validateParam ( ' password ' )-> notNull ();
$ app -> db -> query (...); // etc.
// Add view properties and helper methods
$ service -> title = ' foo ' ;
$ service -> escape = function ( $ str ) {
return htmlentities ( $ str ); // Assign view helpers
};
$ service -> render ( ' myview.phtml ' );
});
// myview.phtml:
<title> <?php echo $ this -> escape ( $ this -> title ) ?> </title>
$ klein -> with ( ' /users ' , function () use ( $ klein ) {
$ klein -> respond ( ' GET ' , ' /? ' , function ( $ request , $ response ) {
// Show all users
});
$ klein -> respond ( ' GET ' , ' /[:id] ' , function ( $ request , $ response ) {
// Show a single user
});
});
foreach ( array ( ' projects ' , ' posts ' ) as $ controller ) {
// Include all routes defined in a file under a given namespace
$ klein -> with ( " / $ controller " , " controllers/ $ controller .php " );
}
包含的檔案在 Klein ( $klein
) 範圍內運行,因此可以使用$this
存取所有 Klein 方法/屬性
範例檔案:“controllers/projects.php”
// Routes to "/projects/?"
$ this -> respond ( ' GET ' , ' /? ' , function ( $ request , $ response ) {
// Show all projects
});
服務可以延遲存儲,這意味著它們僅在第一次使用時實例化。
<?php
$ klein -> respond ( function ( $ request , $ response , $ service , $ app ) {
$ app -> register ( ' lazyDb ' , function () {
$ db = new stdClass ();
$ db -> name = ' foo ' ;
return $ db ;
});
});
//Later
$ klein -> respond ( ' GET ' , ' /posts ' , function ( $ request , $ response , $ service , $ app ) {
// $db is initialised on first request
// all subsequent calls will use the same instance
return $ app -> lazyDb -> name ;
});
若要新增自訂驗證器,請使用addValidator($method, $callback)
$ service -> addValidator ( ' hex ' , function ( $ str ) {
return preg_match ( ' /^[0-9a-f]++$/i ' , $ str );
});
您可以使用is<$method>()
或not<$method>()
驗證參數,例如
$ service -> validateParam ( ' key ' )-> isHex ();
或者您可以使用相同的流程驗證任何字串。
$ service -> validate ( $ username )-> isLen ( 4 , 16 );
驗證方法是可連結的,並且可以為驗證失敗時指定自訂異常訊息
$ service -> validateParam ( ' key ' , ' The key was invalid ' )-> isHex ()-> isLen ( 32 );
[符合類型:參數名稱]
一些例子
* // 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
一些更複雜的例子
/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
注意- 呼叫與請求 URI 相符的所有路由 - 這允許您合併複雜的條件邏輯,例如使用者驗證或視圖佈局。例如,作為一個基本範例,以下程式碼將使用頁首和頁尾包裝其他路由
$ klein -> respond ( ' * ' , function ( $ request , $ response , $ service ) { $ service -> render ( ' header.phtml ' ); });
//other routes
$ klein -> respond ( ' * ' , function ( $ request , $ response , $ service ) { $ service -> render ( ' footer.phtml ' ); });
路由自動匹配整個請求 URI。如果您需要僅符合請求 URI 的一部分或使用自訂正規表示式,請使用@
運算子。如果您需要否定一條路線,請使用!
操作員
// Match all requests that end with '.json' or '.csv'
$ klein ->respond( ' @.(json|csv)$ ' , ...
// Match all requests that _don't_ start with /admin
$ klein ->respond( ' !@^/admin/ ' , ...
您可以透過將屬性或幫助程式指派給$service
物件或使用$service->render()
的第二個參數來將它們傳送到視圖
$ service -> escape = function ( $ str ) {
return htmlentities ( $ str );
};
$ service -> render ( ' myview.phtml ' , array ( ' title ' => ' My View ' ));
// Or just: $service->title = 'My View';
myview.phtml
< title > < ?php echo $this- > escape($this- > title) ? > </ title >
視圖在$service
範圍內編譯和運行,因此可以使用$this
存取所有服務方法
$ this -> render ( ' partial.html ' ) // Render partials
$ this -> sharedData ()-> get ( ' myvar ' ) // Access stored service variables
echo $ this -> query ( array ( ' page ' => 2 )) // Modify the current query string
以下是您最有可能使用的公共類別中的公共方法的清單。有關更正式的類別/方法文件來源,請參閱 PHPdoc 產生的文件。
$ request ->
id ( $ hash = true ) // Get a unique ID for the request
paramsGet() // Return the GET parameter collection
paramsPost() // Return the POST parameter collection
paramsNamed() // Return the named parameter collection
cookies() // Return the cookies collection
server() // Return the server collection
headers() // Return the headers collection
files() // Return the files collection
body() // Get the request body
params() // Return all parameters
params ( $ mask = null ) // Return all parameters that match the mask array - extract() friendly
param ( $ key , $ default = null ) // Get a request parameter (get, post, named)
isSecure() // Was the request sent via HTTPS?
ip() // Get the request IP
userAgent() // Get the request user agent
uri() // Get the request URI
pathname() // Get the request pathname
method() // Get the request method
method ( $ method ) // Check if the request method is $method, i.e. method('post') => true
query ( $ key , $ value = null ) // Get, add to, or modify the current query string
<param> // Get / Set (if assigned a value) a request parameter
$ response ->
protocolVersion ( $ protocol_version = null ) // Get the protocol version, or set it to the passed value
body ( $ body = null ) // Get the response body's content, or set it to the passed value
status() // Get the response's status object
headers() // Return the headers collection
cookies() // Return the cookies collection
code ( $ code = null ) // Return the HTTP response code, or set it to the passed value
prepend ( $ content ) // Prepend a string to the response body
append ( $ content ) // Append a string to the response body
isLocked() // Check if the response is locked
requireUnlocked() // Require that a response is unlocked
lock() // Lock the response from further modification
unlock() // Unlock the response
sendHeaders ( $ override = false ) // Send the HTTP response headers
sendCookies ( $ override = false ) // Send the HTTP response cookies
sendBody() // Send the response body's content
send() // Send the response and lock it
isSent() // Check if the response has been sent
chunk ( $ str = null ) // Enable response chunking (see the wiki)
header ( $ key , $ value = null ) // Set a response header
cookie ( $ key , $ value = null , $ expiry = null ) // Set a cookie
cookie ( $ key , null ) // Remove a cookie
noCache() // Tell the browser not to cache the response
redirect ( $ url , $ code = 302 ) // Redirect to the specified URL
dump ( $ obj ) // Dump an object
file ( $ path , $ filename = null ) // Send a file
json ( $ object , $ jsonp_prefix = null ) // Send an object as JSON or JSONP by providing padding prefix
$ service ->
sharedData() // Return the shared data collection
startSession() // Start a session and return its ID
flash( $ msg , $ type = ' info ' , $ params = array() // Set a flash message
flashes ( $ type = null ) // Retrieve and clears all flashes of $type
markdown( $ str , $ args , ...) // Return a string formatted with markdown
escape ( $ str ) // Escape a string
refresh() // Redirect to the current URL
back() // Redirect to the referer
query ( $ key , $ value = null ) // Modify the current query string
query ( $ arr )
layout ( $ layout ) // Set the view layout
yieldView () // Call inside the layout to render the view content
render ( $ view , $ data = array ()) // Render a view or partial (in the scope of $response)
partial ( $ view , $ data = array ()) // Render a partial without a layout (in the scope of $response)
addValidator ( $ method , $ callback ) // Add a custom validator method
validate ( $ string , $ err = null ) // Validate a string (with a custom error message)
validateParam ( $ param , $ err = null ) // Validate a param
<callback>( $ arg1 , ...) // Call a user-defined helper
<property> // Get a user-defined property
$ app ->
< callback >( $ arg1 , ...) //Call a user-defined helper
$ validator ->
notNull() // The string must not be null
isLen ( $ length ) // The string must be the exact length
isLen ( $ min , $ max ) // The string must be between $min and $max length (inclusive)
isInt() // Check for a valid integer
isFloat() // Check for a valid float/decimal
isEmail() // Check for a valid email
isUrl() // Check for a valid URL
isIp() // Check for a valid IP
isAlpha() // Check for a-z (case insensitive)
isAlnum() // Check for alphanumeric characters
contains ( $ needle ) // Check if the string contains $needle
isChars ( $ chars ) // Validate against a character list
isRegex ( $ pattern , $ modifiers = '' ) // Validate against a regular expression
notRegex ( $ pattern , $ modifiers = '' )
is<Validator>() // Validate against a custom validator
not<Validator>() // The validator can't match
<Validator>() // Alias for is<Validator>()
單元測試是開發 Klein 等路由引擎的關鍵部分。新增的功能或錯誤修復可能會產生負面影響,如果不進行大量測試就很難發現這些負面影響,因此單元測試非常重要。
該專案使用 PHPUnit 作為其單元測試框架。
所有測試都位於/tests
中,每個測試都擴展一個抽象類別AbstractKleinTest
要測試項目,只需運行php composer.phar install --dev
即可下載帶有 Composer 的 PHPUnit 通用版本,並使用./vendor/bin/phpunit
從主目錄運行測試
請參閱貢獻指南以獲取更多信息
請參閱 wiki 以了解更多信息
(麻省理工學院許可證)
版權所有 (c) 2010 克里斯·奧哈拉 [email protected]
特此免費授予任何獲得本軟體及相關文件文件(「軟體」)副本的人不受限制地使用本軟體,包括但不限於使用、複製、修改、合併的權利、發布、散佈、再授權和/或銷售軟體的副本,並允許向其提供軟體的人員這樣做,但須滿足以下條件:
上述版權聲明和本授權聲明應包含在本軟體的所有副本或主要部分中。
本軟體以「現況」提供,不提供任何明示或暗示的保證,包括但不限於適銷性、特定用途的適用性和不侵權的保證。 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軟體.