Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
Practical guide Slim3 framework. In these tutorials, we cover up all basic of a slim framework functionality like Routing, Controller, Middleware, API etc. we also Cover Basic form handling, page navigation, database connectivity, and complete CURD operation.
<?php
// All required dependency
require('vendor/autoload.php');
// Create instance of slim app
$app = new \Slim\App;
// Basic Routing
$app->get('/',function(){
echo 'Hello World!';
});
// Run Instance of Slim App.
$app->run();
?>
<?php
// All required dependency
require('vendor/autoload.php');
// Create instance of slim app
$app = new \Slim\App;
// Basic Routing
$app->get('/',function(){
echo 'Hello World!';
});
// Create Middleware to handle user request
$app->get('/users',function(){
echo 'users';
});
// Create Middleware to handle posts request
$app->get('/posts',function(){
echo 'posts';
});
// Run Instance of Slim App.
$app->run();
?>
Slim uses a dependency container to prepare, manage, and inject application dependencies.
<?php
require('vendor/autoload.php');
$app = new \Slim\App;
$container = $app->getContainer();
$container['myapp'] = function(){
return 'Hello World! from the Container';
};
$app->get('/',function(){
echo $this->myapp;
});
$app->run();
?>
<?php
require('vendor/autoload.php');
$app = new \Slim\App;
$container = $app->getContainer();
$container['view'] = function ($container) {
$view = new \Slim\Views\Twig(__DIR__ .'/resourses/views', [
'cache' => 'false'
]);
$router = $container->get('router');
$uri = \Slim\Http\Uri::createFromEnvironment(new \Slim\Http\Environment($_SERVER));
$view->addExtension(new Slim\Views\TwigExtension($router, $uri));
return $view;
};
$app->get('/',function($request, $response){
return $this->view->render($response, 'home.twig');
});
$app->run();
?>
Index.php
<?php
require('vendor/autoload.php');
$app = new \Slim\App([
'settings' => [
'displayErrorDetails' => true,
]
]);
$container = $app->getContainer();
$container['view'] = function ($container) {
$view = new \Slim\Views\Twig(__DIR__ .'/resourses/views', [
'cache' => false,
]);
$router = $container->get('router');
$uri = \Slim\Http\Uri::createFromEnvironment(new \Slim\Http\Environment($_SERVER));
$view->addExtension(new Slim\Views\TwigExtension($router, $uri));
return $view;
};
$app->get('/',function($request, $response){
return $this->view->render($response, 'home.twig');
});
$app->get('/posts',function($request, $response){
return $this->view->render($response, 'posts.twig');
});
$app->run();
?>
resources/views/layouts/app.twig
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>{% block title %}{% endblock %}</title>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
Index.php
$app->get('/posts',function($request, $response){
return $this->view->render($response, 'posts.twig',[
'Mypost' => 'This is awosome post'
]);
});
show data to twig template
resources/views/posts.twig
{% extends 'layouts/app.twig' %}
{% block title %}Posts{% endblock %}
{% block content %}
<h2>{{ Mypost }}</h2>
{% endblock %}
Index.php
$app->get('/posts',function($request, $response){
return $this->view->render($response, 'posts.twig',[
'Mypost' => 'This is awosome post'
]);
})->setName('posts.show');
on Twig Template
<a href="{{ path_for('posts.show')}}"> Go To Home</a>
$app->get('/posts/{id}',function($request, $response,$args){
$user = [
'id' => $args['id'],
'username' => 'Ajay'
];
return $this->view->render($response, 'contact_confirmed.twig', [
'user' => $user
]);
});
$app->group('/posts',function(){
$this->get('',function(){
echo "All Posts";
});
$this->get('/{id}',function($request, $response, $args){
echo "Get Post " . $args['id'];
});
$this->post('',function(){
echo "Create Posts";
});
});
$container = $app->getContainer();
$container['db'] = function(){
return new PDO('mysql:host=localhost;dbname=ajax;','root','');
};
//FETCH ALL RECORDS
$app->get('/',function($request, $response){
$users = $this->db->query('select * from users')->fetchAll(PDO::FETCH_OBJ);
return $this->view->render($response, 'home.twig',[
'users' => $users
]);
});
return $response->withJson('YOUR DATA',200);
$autenticated = function($request, $response, $next) use ($container){
if(true){
$response = $response->withRedirect('https://localhost:86/slim/public/posts');
}
return $next($request,$response);
};
//use
$app->get('/users', UserController::class.':index')->add($autenticated);