<?php
// Create and configure Slim app
$app = new \Slim\App;
// Define app routes
$app->get('/hello/{name}', function ($request, $response, $args) {
return $response->write("Hello " . $args['name']);
});
// Run app
$app->run();
Slim supports PSR-7 interfaces for its Request and Response objects
Middleware is code that runs before and after your Slim application.
$container = new \Slim\Container;
$app = new \Slim\App($container);
$app->any( '/toolkit/{option}', '\Controllers\MyController:getIndex' );
To retrieve {option}
public function getIndex(Request $request, Response $response, $args) {
...
$option = $args['option'];
...
If your endpoint is /url?keywords=helloworld you would retrieve keywords:
$allGetVars = $request->getQueryParams()
$keywords = $allGetVars['keywords'];
$allPostPutVars = $request->getParsedBody();
$keywords = $allPostPutVars['keywords'];
or
$request->getParam('keywords');
Redirect return $response->withRedirect('/404');
path_for(route,route placeholder names and value)
<a href="{{ path_for('profile', { 'name': 'josh' }) }}">Josh</a>
$route = $request->getAttribute('route');
$name = $route->getName();
$group = $route->getGroup();
$methods = $route->getMethods();
$arguments = $route->getArguments();
Use Middleware package https://github.com/akrabat/rka-ip-address-middleware
$container = new \Slim\Container;
$app = new \Slim\App($container);
// register middleware
$checkProxyHeaders = true;
$trustedProxies = ['10.0.0.1', '10.0.0.2'];
$app->add(new RKA\Middleware\IpAddress($checkProxyHeaders, $trustedProxies));
$view = new \Slim\Views\Twig('../app/templates', [
'cache' => '../storage/cache'
]);
$view = new \Slim\Views\Twig('../app/templates', [
'cache' => null
]);
{% extends "layout.html" %}
{% block content %}
...
{% endblock %}
If your variable has html in it for example, you can tell Twig not to escape this:
{{ item.htmldata() | raw }}
{% for item in posts %}
{% endfor %}
Add to container before calling $app->run();
//Override the default Not Found Handler
$container['notFoundHandler'] = function ($c) {
return function ($request, $response) use ($c) {
return $c['view']->render($response, '404.html', [
"myMagic" => "Let's roll"
]);
};
};
Note make sure you add container when initiating app $app = new \Slim\App($container);