Collective is a skeleton project based on Slim 3, and Twig.
This project comes configured with:
- Symfony Console App
- Slim 3
- Extendable Container Aware Classes (Action, Middleware, Controller)
- Route Loading via config file
- Middleware loading via config file
- Monolog
Middleware:
- Session
- Flash Messages
- Monolog
- Rate Limiting
Rendering:
- Json
- Twig [slim/twig-view]
- PHP [slim/php-view]
Run this command from the directory in which you want to install your new Collective/Slim Framework application.
php composer.phar create-project geggleto/collective [my-app-name]
Replace [my-app-name]
with the desired directory name for your new application.
You'll want to:
- Point your virtual host document root to your new application's
public/
directory. - Ensure
logs/
is web writable.
config/app.config
holds your Dependency Container Default Values.
To turn Twig Caching off:
$container["config"]["cache_path"] = false
Or
"cache_path" => "../cache", ==> "cache_path" => false,
The application expects you to set your web root to the public directory and have
the ability to rewrite URLS. A default .htaccess
is provided.
Collective provides base classes that are container aware so you don't need to write your own!
Collective provides an extendable class Geggleto\Helper\BaseAction
for your Actions
if you are following ADR.
Collective provides an extendable class Geggleto\Helper\BaseContainerClass
for your
Controllers if you are following MVC.
Collective provides an extendable class Geggleto\Helper\BaseMiddleware
for your Middleware.
In any class that extends either (BaseAction
, BaseMiddleware
or BaseContainerClass
),
any dependency listed in app.config
will be available in the class by accessing
it through the app.config
key as a class variable.
That is to say, if I want to access the session object in an action
//Access Session object in a Action
public function __invoke (ServerRequestInterface $request, ResponseInterface $response, array $args)
{
return $this->twig->render($response, "hello.twig", [$this->session->get('name')]);
}
Routes can be configured in the app.config
class for easy configuration.
Each route must have a pattern (key) and a callable element. Middleware and names are optional
'routes' => [
//What HTTP Verb
'get' => [
// / => Pattern
// callable => What action to run
// mw => What middleware are we running
// name => name the route
'/' => [ "callable" => HelloWorldAction::class, "mw" => [], "name" => "" ]
]
]
Application middleware can be configured in the app.config
file as well.
"app-middleware" => [
LoggerMiddleware::class
]
Middleware closures can be added at any point before run by wrapping your closure with a factory closure from pimple.
$this->collective->addMw("Test2", function ($c) {
return function ($req, $res, $next) {
$res = $next($req, $res);
$res->write("Test2");
return $res;
};
}
);
Sessions are on by default.
If you want to turn sessions off or swap packages, then remove the session
key
from the app.config
file. The session class uses whatever your php is configured
to use [which is files by default].
Supported Syntax:
$this->session->get('key');
$this->session->put('key', 'value');
$this->session->has('key');
$this->session->key;
$this->session->key = 'value';
isset($this->session->key);
If you want to use a different storage engine than the see this page.
(http://php.net/manual/en/function.session-set-save-handler.php)
Flash Messages require the Session component. It is enabled by default Flash Messages will be deleted on the next request.
//Create a flash message ... do it someplace
$this->flash->put('message', 'my message');
//Access the message on the next request via ServerRequestInterface
$request->getAttribute('message');
//Access the message in Twig on the next request
{{ flash['message'] }}
Collective provides a Symfony console app for creating Actions and Middleware easily.
php cli.php create:action MyActionClassName
php cli.php create:middleware MyMiddlewareClassName
Custom Error handlers are provided for:
- Page not found
templates/404.twig
- Method not allowed
templates/405.twig
- Server Error
templates/500.twig
... Coming soon