PHP code example of adeptoas / slim3-init

1. Go to this page and download the library: Download adeptoas/slim3-init 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/ */

    

adeptoas / slim3-init example snippets


	$app = new SlimInit();
	

	setDebugHeader(?string $header, string $expectedValue = ''): SlimInit
	

	setException(string $exception, int $statusCode): SlimInit
	

	setException(string $exception, string $exceptionHandlerClass): SlimInit
	

	setException(array $exceptions, int|string $statusCodeOrHandlerClass): SlimInit
	

	/** @var $app \Adepto\Slim3Init\SlimInit */
	$app->addExceptionCallback(function(\Adepto\Slim3Init\Request $request, Throwable $t) {
		// Send $t to the service
	});
	

	class ExceptionCallback {
		public function __invoke(\Adepto\Slim3Init\Request $request, Throwable $t) {
			// Send $t to the service
		}
	}

	/** @var $app \Adepto\Slim3Init\SlimInit */
	$app->addExceptionCallback(new ExceptionCallback());
 	

	addToContainer(string $key, mixed $value): SlimInit
	

	addHandler(string $className): SlimInit
	

	addHandlersFromDirectory(string $dir): SlimInit
	

	addMiddleware(callable $middleware): SlimInit
	

	run(): Slim\App
	

	$caller = new HandlerCaller(string $baseURL, string $handlerClass);
	

	get(string $url, array $headers = []): string
	

	post(string $url, array $headers, mixed $body): string
	

	put(string $url, array $headers, mixed $body, array $files = []): string
	

	patch(string $url, array $headers, mixed $body, $files = []): string
	

	delete(string $url, array $headers, mixed $body): string
	

public function someName(Adepto\Slim3Init\Request $request, Adepto\Slim3Init\Response $response, \stdClass $args): Adepto\Slim3Init\Response

forcePermission(string $action, array $data = []): bool

new Route(string $httpMethod, string $url, string $classMethod, array $arguments = [], string $name = '')

	getUsername(): string
	

	getPermissions(): array
	

	hasPermission(string $name, array $data = []): bool;
	

	getName(): string
	

	getData(): array
	

	isAllowed(): bool
	

	authorize(array $credentials): array
	


/* Slim3 */
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

class YourMiddleware {
	protected $container;

	public function __construct(ContainerInterface $container) {
		$this->container = $container;
	}

	public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Callable $next): ResponseInterface {
		// Something before others run
		$newResponse = $next($request, $response);
		// Code after others have run
		return $newResponse;
	}
}


/* Slim4 */
use Psr\Container\ContainerInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseInterface;
use Adepto\Slim3Init\Request;

class YourMiddleware {
	protected $container;

	public function __construct(ContainerInterface $container) {
		$this->container = $container;
	}

	public function __invoke(Request $request, RequestHandlerInterface $handler): ResponseInterface {
		// Something before others run
		$response = $handler->handle($request);
		// Code after others have run
		return $response;
	}
}


use Adepto\Slim3Init\SlimInit;
use Adepto\Slim3Init\Request;
use Adepto\Slim3Init\Handlers\ExceptionHandler;
use Psr\Http\Message\ResponseInterface;

class NotFoundHandler extends ExceptionHandler {

	public function handle(Request $request, Throwable $t, bool $displayDetails): ResponseInterface {
		return $this->createResponse(404)
		            ->withJson(['error' => 'not_found']);
	}
}

// … your $app definition

/** @var $app SlimInit */
$app->setException(SomethingNotFoundException::class, NotFoundHandler::class);

use Adepto\Slim3Init\Exceptions\InternalErrorException;
use Adepto\Slim3Init\Exceptions\MethodNotAllowedException;
use Adepto\Slim3Init\Exceptions\NotFoundException;
use Adepto\Slim3Init\SlimInit;
/** @var $app SlimInit */

// Customize 404
$app->setException(NotFoundException::class, CustomHandler::class);
// Customize 405
$app->setException(MethodNotAllowedException::class, CustomHandler::class);
// Customize default handler (500)
$app->setException(InternalErrorException::class, CustomHandler::class);
// Customize all at once
$app->setException([
	NotFoundException::class,
	MethodNotAllowedException::class,
	InternalErrorException::class
], CustomHandler::class);

use Adepto\Slim3Init\SlimInit;
// … your $app definition
/** @var $app SlimInit */
$app->setException(SomethingNotFoundException::class, 404);

/** @var $container \Adepto\Slim3Init\Container */
// Get value like normal, with exception if key was not found
$value = $container->get('some-value');
// Get value array-style, with null being returned if key was not found
$value = $container['some-value'];

use Adepto\Slim3Init\Middleware\Middleware;
use Adepto\Slim3Init\Request;
use Adepto\Slim3Init\Response;
use Psr\Http\Server\RequestHandlerInterface;

class YourMiddleware extends Middleware {

	public function __invoke(Request $request, RequestHandlerInterface $handler) : Response{
		return $this->createResponse(404);
	}
}