Namespace: TrishulApi\Core
Author: Shyam Dubey
Since: v1.0.0
The App
class is the main entry point for the Trishul API framework. It is responsible for bootstrapping the application, initializing core components such as routing, exception handling, CORS security, logging, and Swagger documentation. This class should be instantiated and configured in your application's index.php
or main bootstrap file.
use TrishulApi\Core\App;
$app = new App();
// Optionally set a custom global exception handler
$app->set_global_exception_handler(\App\Exception\CustomExceptionHandler::class);
// Optionally set allowed CORS domains
$app->set_allowed_domains(['https://example.com', 'https://api.example.com']);
// Optionally set log directory
$app->set_log_dir('/var/log/trishul');
// Optionally set .env file path
$app->set_env_path(__DIR__ . '/.env');
// Start the application (must be called to handle requests)
$app->start();
public function start(): void
Starts the application by initializing global exception handling, CORS security, and routing. This should be called after all configuration is done.
public function set_global_exception_handler($exception_class): void
Sets a custom global exception handler class. By default, uses ExceptionHandler::class
. Call this before start()
for custom error handling and logging.
Parameter | Type | Description |
---|---|---|
$exception_class | string | Fully qualified class name of the exception handler. |
public function set_allowed_domains($domains): void
Sets the allowed domains for CORS (Cross-Origin Resource Sharing). Accepts an array of domain strings.
Parameter | Type | Description |
---|---|---|
$domains | array | List of allowed domains. |
public function get_allowed_domains(): array
Returns the list of currently allowed CORS domains.
public function set_log_dir($dir): void
Sets the directory where logs will be stored. Throws InvalidArgumentException
if the directory is invalid.
Parameter | Type | Description |
---|---|---|
$dir | string | Path to the log directory. |
public function get_swagger()
Returns the singleton instance of the Swagger documentation generator (TrishulSwagger
).
public function set_env_path($path): void
Sets the path to the environment file (.env
) for configuration.
Parameter | Type | Description |
---|---|---|
$path | string | Path to the .env file. |
public function get_env_path(): string
Returns the path to the environment file currently set.
start()
after all configuration to initialize the application.set_allowed_domains()
to control CORS for your API endpoints.set_log_dir()
to specify where logs should be written.set_env_path()
if your environment file is not in the default location.TrishulApi\Core\Exception\ExceptionHandler
TrishulApi\Core\Http\Router
TrishulApi\Core\Log\LoggerFactory
TrishulApi\Core\Security\CorsSecurity
TrishulApi\Core\Swagger\TrishulSwagger