Trishul API Documentation

Version 1.x.x | Last updated on - 05 Jun 2025

Welcome to the Trishul API documentation for version 1.x.x. This documentation provides detailed information on how to use the Trishul API, including endpoints, request and response formats, and examples.

For the latest updates, please visit our GitHub repository.

Last updated on: 05 Jun 2025

For any issues or contributions, please refer to our GitHub account.

Getting Started

To get started with the Trishul API, you will need to follow these steps:

If your system has php installed and composer installed.

  1. Open terminal and type composer create-project trishulapi/framework app_name
  2. This will install the required files in app_name directory in the current path.
  3. cd app_name
  4. Run the command composer update to update all required dependencies.
  5. Run the command php -S localhost:8000
  6. Application will start at localhost:8000

Project Structure

The Trishul API project structure is designed to be simple and easy to navigate. Below is an overview of the main directories and files:

  • src/ - Contains the application code, including controllers, models, Services etc.
  • vendor/ - Contains all the dependencies installed via Composer.
  • composer.json - The Composer configuration file that lists the dependencies of the project.
  • .env - Environment variables for the application.
  • README.md - A markdown file that provides an overview of the project and how to get started.
  • index.php - The main entry point for the application. This file is responsible for bootstrapping the application and handling requests.

App Initializing

TrishulApi\Core\App class is the main class which bootstraps the application.

  1. Open index.php file and create an object of TrishulApi\Core\App class.
  2. $app = TrishulApi\Core\App();
  3. This class has several useful methods which are used to setup the application correctly.
    • set_exception_handler($class) function is used to set the custom global exception handler for your application. By default TrishulApi manages it.
    • set_allowed_domains(array $domains) used for managing CORS for your application. Add your domains like localhost:4200, https://trishulapi.com, http://example.com. Only these domains will be allowed to interact with the application.
    • set_env_path($filepath) sets the .env path so all the environment properties will be picked from the mentioned file.
    • set_log_dir($dir) takes one param $dir. This is used to set the directory to create and save the logging messages in files (auto generates day wise).
    • get_swagger() returns the object of TrishulSwagger class which is used to generate swagger documentation. You can then use methods of TrishulSwagger class to manage the swagger properties.
    • start() function should be added in the last of index.php file so that all the configuration is loaded correctly.

For more information on App class, please refer to the App documentation.

Create Routes

To create routes in the Trishul API, you will need to follow these steps:

TrishulApi\Core\Http\Router class is responsible for creating routes.

  1. Open index.php file and add a route
  2. Use the Router::get() method to create a GET route.

use TrishulApi\Core\Http\Router;
Router::get('/hello', ["hello"=>"world"]); 

In the above example, we have created a GET route for the path /hello that returns a JSON response {"hello":"world"}.

You can also create POST, PUT, DELETE routes using the respective methods Router::post(), Router::put(), and Router::delete().

For more information on creating routes, please refer to the Router documentation.

Request

To handle requests in the Trishul API, you can use the TrishulApi\Core\Http\Request class. This class provides methods to access request data, headers, and other information related to the incoming HTTP request.

Here is an example of how to use the Request class:

TrishulApi provides Request class object on the class which you use in Router::get("/", Controller::class."@method").

In such case Request class object will automatically be injected in the Constructor of Controller class.

Class Controller {
    public function __construct(TrishulApi\Core\Http\Request $request) {
        // You can access request data here
        $this->request = $request;
    }

    public function method() {
        // Access request data
        $data = $this->request->header()->get('key'); // Get a specific key from the request header
        
        $body = $this->request->body(); // Get the request body
        $queryParams = $this->request->query_params(); // Get query parameters from the URL
    }
}

For more information on Request Class, please refer to the Request documentation.

Response

p class="large text-muted">To handle responses in the Trishul API, you can use the TrishulApi\Core\Http\Response class. This class provides methods to set the response status code, headers, and body content. It also allows you to send JSON responses easily.

Here is an example of how to use the Response class:

Class Controller {
    public function __construct(TrishulApi\Core\Http\Request $request) {
        // You can access request data here
        $this->request = $request;
    }

    public function method() {
        return Response::json(HttpStatus::OK, [
            'message' => 'Hello, World!',
            'data' => ['key' => 'value']
        ]);
    }
}

For more information on Response Class, please refer to the Response documentation.

Logging

p class="large text-muted">Trishul API provides a simple logging mechanism to log messages to files. You can use the TrishulApi\Core\Log\LoggerFactory class to log messages at different levels (info, warning, error, etc.).

Here is an example of how to use the LoggerFactory class:

use TrishulApi\Core\Log\LoggerFactory;
$logger = LoggerFactory::get_instance(Controller::class);
$logger->info('This is an info message');
$logger->warn('This is a warning message');
$logger->error('This is an error message');

For more information on LoggerFactory Class, please refer to the LoggerFactory documentation.

MiddlewareInterface

Trishul API allows you to create middleware to handle requests and responses before they reach the controller. You can implement the TrishulApi\Core\Http\MiddlewareInterface interface to create your own middleware.

Here is an example of how to create a middleware:

use TrishulApi\Core\Http\MiddlewareInterface;
class MyMiddleware implements MiddlewareInterface {
    public function handle(TrishulApi\Core\Http\Request $request):Request {
        // Perform some action before the request is processed
        $request->header()->set('X-Custom-Header', 'MyValue');
        // Call the next middleware or controller
        return $request;
    }

    public function handle_response(TrishulApi\Core\Http\Response $response):Response {
        // Perform some action before the response is sent
        $response->header()->set('X-Custom-Response-Header', 'MyValue');
        // Return the modified response
        return $response;
    }
                        

For more information on MiddlewareInterface Class, please refer to the MiddlewareInterface documentation.

DB

Trishul API provides a class for providing PDO object.

Here is an example of how to use the TrishulApi\Core\Data\DB class:

use TrishulApi\Core\Data\DB;
$pdo = DB::get_connection();
// Use the PDO object to execute queries
$statement = $pdo->prepare('SELECT * FROM users WHERE id = :id');
$statement->execute(['id' => 1]);
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
            

For more information on DB Class, please refer to the DB documentation.

Model

Trishul API provides a base model class that you can extend to create your own models. The base model class provides methods for interacting with the database using PDO.

Here is an example of how to create a model:

use TrishulApi\Core\Data\Model;
class User extends Model {
    public string $table_name = 'users'; // Specify the table name
    public string $primary_key = 'user_id'; // Define primary key for the model default is id
    public function __construct() {
        parent::__construct();
    }
}
            

Model class provides several functions to extract the data from the database.

For more information on Model Class, please refer to the Model documentation.

TrishulClient

Trishul API provides a client class that you can use to make HTTP requests to other APIs. The TrishulApi\Core\Http\TrishulClient class provides methods for making GET, POST, PUT, DELETE requests.

Here is an example of how to use the TrishulClient class:

use TrishulApi\Core\Http\TrishulClient;
$client = new TrishulClient();
$response = $client->get_mono('https://api.example.com/data');
            

For more information on TrishulClient Class, please refer to the TrishulClient documentation.