1. Go to this page and download the library: Download responsive-sk/slim4-root library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
responsive-sk / slim4-root example snippets
use Slim4\Root\Paths;
// Create a new Paths instance with auto-discovery enabled
$paths = new Paths(__DIR__);
// Get paths relative to the root
$configPath = $paths->getConfigPath();
$viewsPath = $paths->getViewsPath();
$logsPath = $paths->getLogsPath();
// Get all paths at once
$allPaths = $paths->getPaths();
// No more ../../../ paths!
// Instead of:
//
use Slim4\Root\PathsDiscoverer;
// Create a new PathsDiscoverer instance
$discoverer = new PathsDiscoverer();
// Discover paths
$discoveredPaths = $discoverer->discover(__DIR__);
// Use discovered paths
var_dump($discoveredPaths);
use Slim4\Root\PathsValidator;
use Slim4\Root\Exception\InvalidPathException;
// Create a new PathsValidator instance
$validator = new PathsValidator();
// Validate paths
try {
$validator->validate([
'config' => __DIR__ . '/config',
'views' => __DIR__ . '/views',
], true); // Strict validation (throws exception if path doesn't exist)
} catch (InvalidPathException $e) {
echo $e->getMessage();
}
use Slim4\Root\PathsNormalizer;
// Create a new PathsNormalizer instance
$normalizer = new PathsNormalizer();
// Normalize paths
$normalizedPath = $normalizer->normalize('C:\\path\\to\\project\\');
// Returns: 'C:/path/to/project'
use Slim4\Root\PathsProvider;
use DI\ContainerBuilder;
// Create container
$containerBuilder = new ContainerBuilder();
// Register paths services
$rootPath = dirname(__DIR__);
PathsProvider::register(
$containerBuilder->build(),
$rootPath,
[], // Custom paths
true, // Enable auto-discovery
false // Disable path validation
);
// Get paths from the container
$paths = $container->get(Slim4\Root\PathsInterface::class);
use Slim4\Root\PathsMiddleware;
use Slim\Factory\AppFactory;
// Create app
$app = AppFactory::createFromContainer($container);
// Add the middleware to the app
$app->add($container->get(PathsMiddleware::class));
// Access paths in a route handler
$app->get('/', function ($request, $response) {
$paths = $request->getAttribute('paths');
$configPath = $paths->getConfigPath();
// ...
return $response;
});
use Slim4\Root\BasePathMiddleware;
use Slim\Factory\AppFactory;
// Create app
$app = AppFactory::createFromContainer($container);
// Add the middleware to detect and set the base path
$app->add(new BasePathMiddleware($app));
// Now all routes will work correctly even if your app is in a subdirectory
$app->get('/', HomeAction::class);
use Slim\Views\Twig;
use Slim4\Root\PathsInterface;
// Register Twig with the container
$container->set(Twig::class, function (ContainerInterface $container) {
$paths = $container->get(PathsInterface::class);
$twig = Twig::create($paths->getViewsPath(), [
'cache' => $paths->getCachePath() . '/twig',
'debug' => true,
'auto_reload' => true,
]);
return $twig;
});
use Monolog\Level;
use Slim4\Root\Integration\MonologFactory;
use Slim4\Root\PathsInterface;
use Psr\Log\LoggerInterface;
// Register Logger with the container
$container->set(LoggerInterface::class, function (ContainerInterface $container) {
$paths = $container->get(PathsInterface::class);
$factory = new MonologFactory($paths);
return $factory->createLogger([
'name' => 'app',
'path' => 'app.log',
'level' => Level::Debug,
'rotating' => true,
'max_files' => 7,
]);
});
use Slim4\Root\Testing\TestContainer;
use Slim4\Root\Paths;
// In your bootstrap file
$paths = new Paths(__DIR__);
TestContainer::set(Paths::class, $paths);
// In your tests
$paths = TestContainer::get(Paths::class);