1. Go to this page and download the library: Download horizom/core 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/ */
horizom / core example snippets
e('HORIZOM_ROOT', __DIR__);
use Horizom\Core\App;
use Horizom\Http\Request;
$app = new App(basePath: __DIR__);
// Register service providers
$app->register(\Horizom\Core\Providers\CoreServiceProvider::class);
// Add global middlewares
$app->add(\Horizom\Core\Middlewares\BodyParsingMiddleware::class);
$app->add(\Horizom\Core\Middlewares\ContentLengthMiddleware::class);
// Define routes through the router
$app->getRouter()->get('/', function () {
return view('home', ['title' => 'Horizom']);
});
$app->run(Request::createFromGlobals());
use Horizom\Core\App;
use Horizom\Http\Request;
$app = new App(basePath: __DIR__);
// Singleton - retrieve the existing instance
$app = App::getInstance();
// Framework version
echo $app->version(); // "4.0.0"
// Add a middleware to the global pipeline
$app->add(new \Horizom\Core\Middlewares\ContentLengthMiddleware());
$app->add(MyCustomMiddleware::class); // resolved through the container
// Read all configuration
$all = $app->config();
// Load a configuration file
// Loads config/database.php and merges the keys
$app->configure('database');
// Write one or more values
$app->config(['app.name' => 'My App', 'app.env' => 'production']);
// Runs the pipeline, dispatches the request, emits the HTTP response
$app->run(Request::createFromGlobals());
use Horizom\Core\Facades\Config;
// Read with a default value
$name = Config::get('app.name', 'MyApp');
// Write
Config::put('app.debug', true);
// Check whether a key exists
Config::has('app.timezone'); // true
// Read through the helper
$tz = config('app.timezone', 'UTC');
use Horizom\Core\Container;
use DI\ContainerBuilder;
$builder = new ContainerBuilder(Container::class);
$container = $builder->useAutowiring(true)->build();
// Register a provider
$provider = new MyServiceProvider($app);
$container->register($provider);
// Check whether a provider is loaded
$container->isLoaded(MyServiceProvider::class); // bool
// Retrieve a registered provider
$provider = $container->getProvider(MyServiceProvider::class);
// Boot all providers
$container->boot();
declare(strict_types=1);
namespace App\Providers;
use Horizom\Core\ServiceProvider;
class DatabaseServiceProvider extends ServiceProvider
{
public function register(): void
{
// Bind interfaces to their implementations
$this->app->singleton(
\App\Contracts\DatabaseInterface::class,
\App\Services\PdoDatabase::class
);
}
public function boot(): void
{
// Code executed after all providers have been registered
$db = $this->app->get(\App\Contracts\DatabaseInterface::class);
$db->connect();
}
public function provides(): array
{
return [\App\Contracts\DatabaseInterface::class];
}
}
use Horizom\Core\Facades\Config;
use Horizom\Core\Facades\Request;
use Horizom\Core\Facades\Response;
use Horizom\Core\Facades\View;
// Config
$locale = Config::get('app.locale');
// Request
$email = Request::input('email');
$token = Request::bearerToken();
// Response
return Response::json(['status' => 'ok']);
return Response::redirect('/dashboard', 302);
// View
$html = View::render('emails.welcome', ['user' => $user]);
declare(strict_types=1);
namespace App\Facades;
use Horizom\Core\Facades\Facade;
class Cache extends Facade
{
public static function getFacadeAccessor(): string
{
return \App\Services\CacheService::class;
}
}
use Horizom\Core\Middlewares\BodyParsingMiddleware;
$middleware = new BodyParsingMiddleware();
// Register a custom parser
$middleware->registerBodyParser('application/msgpack', function (string $body): array {
return msgpack_unpack($body);
});
$app->add($middleware);
use Horizom\Core\Contracts\ExceptionHandler;
use Horizom\Core\Middlewares\ExceptionHandlingMiddleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
class MyExceptionHandler implements ExceptionHandler
{
public function handle(
\Throwable $e,
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
// Build a custom error response
}
}
$app->instance(ExceptionHandler::class, new MyExceptionHandler());
$app->add(new ExceptionHandlingMiddleware($app->get(ExceptionHandler::class)));