Last active
January 31, 2017 10:05
-
-
Save samayo/97b665f0cc6823b82670 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
### USAGE | |
#Require the autoload in your bootstrap, and use the Application to start your app | |
require __DIR__ . '/../Autoload.php'; | |
$app = new Fastpress\Application; | |
$app->get('/hello/{:name}', function($name) use ($app){ | |
echo 'Hello ' $app->escape($name); // Hello world | |
}); | |
$app->run(); | |
###### Simple blog Example. | |
$app->get('/blog/{:slug}', function($slug) use ($app, $blogRepository){ | |
//.. Assuming have a class that fetchs blogs by slug, id, date ... | |
$blog = $blogRepository->getBySlug($slug); | |
if(!$blog){ | |
$app->response(404, "Not Found"); // returns a 404 page | |
} | |
// if blog is found, render it in your view. | |
$app->view("page.blogs", ["blog" => $blog]); | |
}); | |
###### User Login Example | |
// we call the 'any()' method to use both GET or POST in this page. | |
$app->any("/login", function() use ($app){ | |
$error = NULL; | |
if($app->isPost()){ | |
$email = $app->postVar("email", FILTER_VALIDATE_EMAIL); | |
$pass = $app->postVar("password"); | |
if(!$email || !$pass){ | |
$error = "email and password is required"; | |
}else{ | |
// database/password stuff goes here | |
if($findUser){ | |
$app->session("user", md5($email)); | |
$app->redirect("/secure-page"); | |
} | |
} | |
} | |
// errors (if any) will be shown in app/views/page.login.php file | |
$app->view("page.login", ["error" => $error]); | |
}); | |
###### MVC style | |
//Using the MVC pattern is very simple as seen below. | |
// put this inside your src/app/bootstrap.php | |
$app = new Fastpress\Application; | |
$app->get('/user/{:name}', 'UserController@index'); | |
$app->run(); | |
// and create a 'UserController' in app/controller/ | |
namespace App\Controller; | |
class UserController{ | |
public function index($name, $app){ | |
echo "Hello " . $app->escape($name); // hello name | |
// the $app, and the {:name} vars are passed to your methods. | |
} | |
} | |
###### settings everywhere, everything, every-time | |
//You can use the `$app` or `$app->set()` method to add/override any config in your app. | |
# go to url http://your-host/blog/why-php-rocks | |
$app->get('/blog/{:slug}', function($slug) use ($app){ | |
$app->set("page:title", $slug); | |
// Now in your layout the title is changed to: why-php-rocks | |
// maybe you don't want to show adsense for this page? | |
$app->set("use:adsense", false); | |
// or you want to hide the sidebar instead | |
$app->set("block:sidebar", false); | |
$app->set("cache:page", $app->getUri()); #@TODO | |
}); | |
#### Adding classes / libs | |
// Using other (or your own custom) lib/class is as simple as shown here. | |
// If you are not using composer, just drop the class anywhere in `/src/lib/` | |
// let's include a class called Autolog.php it has its own folder called 'Logger' | |
// so now we have /src/lib/Logger/autolog.php | to use it, do: | |
use Fastpress\Logger\Autolog as Log; | |
$app->any('/blog/{:id}', function($id) use ($app){ | |
// check if form post is made, and get post contents | |
if($app->isPost()){ | |
$username = $app->postVar('username'); | |
$comment = $app->postVar('comment'); | |
$email = $app->postVar('email', FILTER_VALIDATE_EMAIL); | |
if($username && $comment && $email){ | |
// .. database stuff .. | |
$user = $app->escape($username); | |
return (new Log) | |
->log(" $user just commented on page " . $app->getUri(), Log::EMAIL, log::INFO); | |
} | |
} | |
}); | |
// Don't want to instantiate objects? Include it in `/lib/container.php` and use it as | |
$app->log("$user just commented on page.", 'EMAIL', 'INFO'); | |
// now the syntax is a lot simpler. | |
#### Views and template-inheritance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment