|
| 1 | +# Slim Framework for PHP 5 |
| 2 | + |
| 3 | +Slim is a micro framework for PHP 5 that helps you quickly write simple yet powerful RESTful web applications and APIs. Slim is easy to use for both beginners and professionals. Slim favors cleanliness over terseness and common cases over edge cases. Its interface is simple, intuitive, and extensively documented — both online and in the code itself. Thank you for choosing Slim for your next project. I think you're going to love it. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +* Clean and simple [DSL](http://en.wikipedia.org/wiki/Domain-specific_language) for writing powerful web applications |
| 8 | +* HTTP routing |
| 9 | + * Supports all standard and custom HTTP request methods |
| 10 | + * Route parameters and conditions |
| 11 | + * Route redirects |
| 12 | + * Route passing |
| 13 | + * Route halting |
| 14 | + * Route middleware |
| 15 | + * Named routes and `urlFor()` helper |
| 16 | +* Easy configuration |
| 17 | +* Easy templating with custom Views (e.g. Twig, Mustache, Smarty) |
| 18 | +* Flash messaging |
| 19 | +* Signed cookies with AES-256 encryption |
| 20 | +* HTTP caching (ETag and Last-Modified) |
| 21 | +* Logging |
| 22 | +* Error handling |
| 23 | + * Custom Not Found handler |
| 24 | + * Custom Error handler |
| 25 | + * Debugging |
| 26 | +* Built upon the Rack protocol |
| 27 | +* Extensible middleware and hook architecture |
| 28 | +* Supports PHP >= 5.2.0 |
| 29 | + |
| 30 | +## "Hello World" application (PHP >= 5.3) |
| 31 | + |
| 32 | +The Slim Framework for PHP 5 supports anonymous functions. This is the preferred method to define Slim application routes. This example assumes you have setup URL rewriting with your web server (see below). |
| 33 | + |
| 34 | +```php |
| 35 | +<?php |
| 36 | +require 'Slim/Slim.php'; |
| 37 | +$app = new Slim(); |
| 38 | +$app->get('/hello/:name', function ($name) { |
| 39 | + echo "Hello, $name!"; |
| 40 | +}); |
| 41 | +$app->run(); |
| 42 | +``` |
| 43 | + |
| 44 | +## "Hello World" application (PHP < 5.3) |
| 45 | + |
| 46 | +If you are running PHP < 5.3, the second argument to the application's `get()` instance method is the name of a callable function instead of an anonymous function. This example assumes you have setup URL rewriting with your web server (see below). |
| 47 | + |
| 48 | +```php |
| 49 | +<?php |
| 50 | +require 'Slim/Slim.php'; |
| 51 | +$app = new Slim(); |
| 52 | +$app->get('/hello/:name', 'hello'); |
| 53 | +function hello($name) { |
| 54 | + echo "Hello, $name!"; |
| 55 | +} |
| 56 | +$app->run(); |
| 57 | +``` |
| 58 | + |
| 59 | +## Get Started |
| 60 | + |
| 61 | +### Install Slim |
| 62 | + |
| 63 | +Download the Slim Framework for PHP 5 and unzip the downloaded file into your virtual host's public directory. Slim will work in a sub-directory, too. |
| 64 | + |
| 65 | +### Setup your webserver |
| 66 | + |
| 67 | +#### Apache |
| 68 | + |
| 69 | +Ensure the `.htaccess` and `index.php` files are in the same public-accessible directory. The `.htaccess` file should contain this code: |
| 70 | + |
| 71 | + RewriteEngine On |
| 72 | + RewriteCond %{REQUEST_FILENAME} !-f |
| 73 | + RewriteRule ^ index.php [QSA,L] |
| 74 | + |
| 75 | +#### Nginx |
| 76 | + |
| 77 | +Your nginx configuration file should contain this code (along with other settings you may need) in your `location` block: |
| 78 | + |
| 79 | + if (!-f $request_filename) { |
| 80 | + rewrite ^ /index.php last; |
| 81 | + } |
| 82 | + |
| 83 | +This assumes that Slim's `index.php` is in the root folder of your project (www root). |
| 84 | + |
| 85 | +#### lighttpd #### |
| 86 | + |
| 87 | +Your lighttpd configuration file should contain this code (along with other settings you may need). This code requires lighttpd >= 1.4.24. |
| 88 | + |
| 89 | + url.rewrite-if-not-file = ("^" => "/index.php") |
| 90 | + |
| 91 | +This assumes that Slim's `index.php` is in the root folder of your project (www root). |
| 92 | + |
| 93 | +### Build Your Application |
| 94 | + |
| 95 | +Your Slim application will be defined in `index.php`. First, `require` the Slim Framework: |
| 96 | + |
| 97 | +```php |
| 98 | +require 'Slim/Slim.php'; |
| 99 | +``` |
| 100 | + |
| 101 | +Next, initialize the Slim application: |
| 102 | + |
| 103 | +```php |
| 104 | +$app = new Slim(); |
| 105 | +``` |
| 106 | + |
| 107 | +Next, define your application's routes: |
| 108 | + |
| 109 | +```php |
| 110 | +$app->get('/hello/:name', function ($name) { |
| 111 | + echo "Hello $name"; |
| 112 | +}); |
| 113 | +``` |
| 114 | + |
| 115 | +Finally, run your Slim application: |
| 116 | + |
| 117 | +```php |
| 118 | + $app->run(); |
| 119 | +``` |
| 120 | + |
| 121 | +For more information about building an application with the Slim Framework, refer to the [official documentation](http://github.com/codeguy/Slim/wiki/Slim-Framework-Documentation). |
| 122 | + |
| 123 | +## Documentation |
| 124 | + |
| 125 | +* [Stable Branch Documentation](http://www.slimframework.com/documentation/stable) |
| 126 | +* [Development Branch Documentation](http://www.slimframework.com/documentation/develop) |
| 127 | + |
| 128 | +## Community |
| 129 | + |
| 130 | +### Forum and Knowledgebase |
| 131 | + |
| 132 | +Visit Slim's official forum and knowledge base at <http://help.slimframework.com> where you can find announcements, chat with fellow Slim users, ask questions, help others, or show off your cool Slim Framework apps. |
| 133 | + |
| 134 | +### Twitter |
| 135 | + |
| 136 | +Follow [@slimphp](http://www.twitter.com/slimphp) on Twitter to receive the very latest news and updates about the framework. |
| 137 | + |
| 138 | +### IRC |
| 139 | + |
| 140 | +You can find me, Josh Lockhart, hanging out in the ##slim chat room during the day. Feel free to say hi, ask questions, or just hang out. If you're on a Mac, check out Colloquy; if you're on a PC, check out mIRC; if you're on Linux, I'm sure you already know what you're doing. |
| 141 | + |
| 142 | +## Resources |
| 143 | + |
| 144 | +Additional resources (ie. custom Views and plugins) are available online in a separate repository. |
| 145 | + |
| 146 | +<https://github.com/codeguy/Slim-Extras> |
| 147 | + |
| 148 | +Here are more links that may also be useful. |
| 149 | + |
| 150 | +* Road Map: <http://github.com/codeguy/Slim/wiki/Road-Map> |
| 151 | +* Source Code: <http://github.com/codeguy/Slim/> |
| 152 | + |
| 153 | +## About the Author |
| 154 | + |
| 155 | +Slim is created and maintained by Josh Lockhart, a web developer by day at [New Media Campaigns](http://www.newmediacampaigns.com), and a [hacker by night](http://github.com/codeguy). |
| 156 | + |
| 157 | +Slim is in active development, and test coverage is continually improving. |
| 158 | + |
| 159 | +## Open Source License |
| 160 | + |
| 161 | +Slim is released under the MIT public license. |
| 162 | + |
| 163 | +<http://www.slimframework.com/license> |
0 commit comments