forked from Tieno/SlimPackage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
258 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,153 +1,104 @@ | ||
# Slim Framework for PHP 5 | ||
|
||
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. | ||
|
||
## Features | ||
|
||
* Clean and simple [DSL](http://en.wikipedia.org/wiki/Domain-specific_language) for writing powerful web applications | ||
* HTTP routing | ||
* Supports all standard and custom HTTP request methods | ||
* Route parameters and conditions | ||
* Route redirects | ||
* Route passing | ||
* Route halting | ||
* Route middleware | ||
* Named routes and `urlFor()` helper | ||
* Easy configuration | ||
* Easy templating with custom Views (e.g. Twig, Mustache, Smarty) | ||
* Flash messaging | ||
* Signed cookies with AES-256 encryption | ||
* HTTP caching (ETag and Last-Modified) | ||
* Logging | ||
* Error handling | ||
* Custom Not Found handler | ||
* Custom Error handler | ||
* Debugging | ||
* Built upon the Rack protocol | ||
* Extensible middleware and hook architecture | ||
* Supports PHP >= 5.2.0 | ||
|
||
## "Hello World" application (PHP >= 5.3) | ||
|
||
The Slim Framework for PHP 5 supports anonymous functions. This is the preferred method to define Slim application routes. | ||
|
||
<?php | ||
require 'Slim/Slim.php'; | ||
$app = new Slim(); | ||
$app->get('/hello/:name', function ($name) { | ||
echo "Hello, $name!"; | ||
}); | ||
$app->run(); | ||
?> | ||
|
||
## "Hello World" application (PHP < 5.3) | ||
|
||
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. | ||
|
||
<?php | ||
require 'Slim/Slim.php'; | ||
$app = new Slim(); | ||
$app->get('/hello/:name', 'hello'); | ||
function hello($name) { | ||
echo "Hello, $name!"; | ||
} | ||
$app->run(); | ||
?> | ||
|
||
## Get Started | ||
|
||
### Install Slim | ||
|
||
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. | ||
|
||
### Setup your webserver | ||
|
||
#### Apache | ||
|
||
Ensure the `.htaccess` and `index.php` files are in the same public-accessible directory. The `.htaccess` file should contain this code: | ||
|
||
RewriteEngine On | ||
RewriteCond %{REQUEST_FILENAME} !-f | ||
RewriteRule ^ index.php [QSA,L] | ||
|
||
#### Nginx | ||
|
||
Your nginx configuration file should contain this code (along with other settings you may need) in your `location` block: | ||
|
||
if (!-f $request_filename) { | ||
rewrite ^ /index.php last; | ||
# SlimPackage | ||
SlimPackage is a bundling of the following projects | ||
|
||
* Model: PHPActiveRecord ([kla/php-activerecord](https://github.com/kla/php-activerecord)) | ||
* View: Twig ([fabpot/Twig](https://github.com/fabpot/Twig)) | ||
* Controller: Slim ([codeguy/Slim](https://github.com/codeguy/Slim)) | ||
|
||
## Adjustments/hacks | ||
To get these three layers working together or for personal enjoyment the following adjustments/hacks were made: | ||
|
||
### 1. To vendor/php-activerecord/lib/model.php | ||
to make PHPActiveRecord work with the Twig templating engine | ||
<?php | ||
/** | ||
* Determines if an attribute exists for this {@link Model}. | ||
* | ||
* @param string $attribute_name | ||
* @return boolean | ||
*/ | ||
public function __isset($attribute_name) | ||
{ | ||
return array_key_exists($attribute_name,$this->attributes) | ||
|| array_key_exists($attribute_name,static::$alias_attribute) | ||
|| method_exists($this, "attributes"); | ||
} | ||
|
||
/** | ||
* Updates a model's timestamps. | ||
*/ | ||
public function set_timestamps() | ||
{ | ||
$now = date('Y-m-d H:i:s'); | ||
try { | ||
$this->updated_at = $now; | ||
} catch (UndefinedPropertyException $e) { | ||
} | ||
try { | ||
if($this->is_new_record()) { | ||
$this->created_at = $now; | ||
} | ||
} catch (UndefinedPropertyException $e) { | ||
} | ||
|
||
} | ||
|
||
### 2. incorporated [Slim-Extras](https://github.com/codeguy/Slim-Extras)/TwigView.php | ||
This is an extension to get the Slim framework to work with Twig templating engine | ||
|
||
#### 2.1 To the TwigView extension the following adjustments were made | ||
|
||
##### 2.1.1 Added a static twigFunctions array for easy setting of TwigFunctions and easy loading of Twigfunctions in getEnvironment() | ||
|
||
/** | ||
* @var TwigFunction the custom functions you want to load | ||
* @param functionName alias for the function | ||
* @param function the actual function (can be a static class method) | ||
*/ | ||
public static $twigFunctions = array(); | ||
/** | ||
* @var TwigEnvironment The Twig environment for rendering templates. | ||
*/ | ||
private $twigEnvironment = null; | ||
|
||
/** | ||
* Creates new TwigEnvironment if it doesn't already exist, and returns it. | ||
* | ||
* @return Twig_Environment | ||
*/ | ||
public function getEnvironment() { | ||
if ( !$this->twigEnvironment ) { | ||
require_once self::$twigDirectory . '/Autoloader.php'; | ||
Twig_Autoloader::register(); | ||
$loader = new Twig_Loader_Filesystem($this->getTemplatesDirectory()); | ||
$this->twigEnvironment = new Twig_Environment( | ||
$loader, | ||
self::$twigOptions | ||
); | ||
foreach (self::$twigFunctions as $function) { | ||
$this->twigEnvironment->addFunction($function['functionName'], new Twig_Function_Function($function['function'])); | ||
} | ||
$extension_autoloader = dirname(__FILE__) . '/Extension/TwigAutoloader.php'; | ||
if (file_exists($extension_autoloader)) { | ||
require_once $extension_autoloader; | ||
Twig_Extensions_Autoloader::register(); | ||
|
||
foreach (self::$twigExtensions as $ext) { | ||
$this->twigEnvironment->addExtension(new $ext); | ||
} | ||
} | ||
} | ||
return $this->twigEnvironment; | ||
} | ||
|
||
This assumes that Slim's `index.php` is in the root folder of your project (www root). | ||
|
||
#### lighttpd #### | ||
|
||
Your lighttpd configuration file should contain this code (along with other settings you may need). This code requires lighttpd >= 1.4.24. | ||
|
||
url.rewrite-if-not-file = ("^" => "/index.php") | ||
|
||
This assumes that Slim's `index.php` is in the root folder of your project (www root). | ||
|
||
### Build Your Application | ||
|
||
Your Slim application will be defined in `index.php`. First, `require` the Slim Framework: | ||
|
||
require 'Slim/Slim.php'; | ||
|
||
Next, initialize the Slim application: | ||
|
||
$app = new Slim(); | ||
|
||
Next, define your application's routes: | ||
|
||
$app->get('/hello/:name', function ($name) { | ||
echo "Hello $name"; | ||
}); | ||
|
||
Finally, run your Slim application: | ||
|
||
$app->run(); | ||
|
||
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). | ||
|
||
## Documentation | ||
|
||
* [Stable Branch Documentation](http://www.slimframework.com/documentation/stable) | ||
* [Development Branch Documentation](http://www.slimframework.com/documentation/develop) | ||
|
||
## Community | ||
|
||
### Forum | ||
|
||
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. | ||
|
||
|
||
Follow [@slimphp](http://www.twitter.com/slimphp) on Twitter to receive the very latest news and updates about the framework. | ||
|
||
### IRC | ||
|
||
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. | ||
|
||
## Resources | ||
|
||
Additional resources (ie. custom Views and plugins) are available online in a separate repository. | ||
|
||
<https://github.com/codeguy/Slim-Extras> | ||
|
||
Here are more links that may also be useful. | ||
|
||
* Road Map: <http://github.com/codeguy/Slim/wiki/Road-Map> | ||
* Source Code: <http://github.com/codeguy/Slim/> | ||
|
||
## About the Author | ||
|
||
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). | ||
|
||
Slim is in active development, and test coverage is continually improving. | ||
|
||
## Open Source License | ||
|
||
Slim is released under the MIT public license. | ||
|
||
<http://www.slimframework.com/license> | ||
##### 2.1.2 getRender() function added just to catch the result of a template render | ||
|
||
public function getRender($template, $data) { | ||
$env = $this->getEnvironment(); | ||
$template = $env->loadTemplate($template); | ||
$data = array_merge($data, $this->data); | ||
return $template->render($data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
<?php | ||
|
||
|
||
ActiveRecord\Config::initialize(function($cfg) | ||
{ | ||
$models = 'app/models'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.