Skip to content

Instantly share code, notes, and snippets.

@samayo
Created August 17, 2015 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samayo/2448c92b015beacceb09 to your computer and use it in GitHub Desktop.
Save samayo/2448c92b015beacceb09 to your computer and use it in GitHub Desktop.
sketch
### 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 = 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
Fastpress supports template inheritance-like feature
// If the URL is a match, This will include the file '/views/user.profile.php'
$app->get('/pages/contact', function() use ($app){
$app->view('user.profile');
})
That file is a block file, that extends a main layout page, so you must describe
which layout you want to extend.
// in app/views/user.profile.php
phpphp $this->extend('master')->block('block:header') ?>
<h3> I'm inside the layout now </h3>
// make sure to close the buffer using endBlock();
phpphp $this->endBlock(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment