# Installation

### Requirements

* PHP >= 7.4
* CodeIgniter >= 4

### Installation

Install the latest version using the composer command below:

```bash
composer require inspector-apm/inspector-codeigniter
```

Run the install command to publish the `Inspector.php` configuration file in your application `app/Config` directory:

```bash
php spark inspector:install
```

### Configure The Ingestion Key

Add the environment variable below to your `.env` file in order to make your application able to send data to your dashboard. You can get a new Ingestion Key by creating a new app in your account: [https://app.inspector.dev](https://app.inspector.dev/)

```
#--------------------------------------------------------------------
# INSPECTOR
#--------------------------------------------------------------------

inspector.ingestionKey = '974yn8c34ync8xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
```

### Add The Exception Handler

In your `app/Config/Exceptions.php` add the line of code below in the `handle` method to enable exception tracking:

```php
/**
 * Setup how the exception handler works.
 */
class Exceptions extends BaseConfig
{
    ...

    public function handler(int $statusCode, Throwable $exception): ExceptionHandlerInterface
    {
        InspectorExceptionHandler::handle($statusCode, $exception);

        return new ExceptionHandler($this);
    }
}

```

### HTTP Request Monitoring

The package provides your application with the `inspector` filter that you can use to monitor incoming HTTP requests.

**We highly recommend** to add `inspector` to the global filters in your `app/Config/Filter.php` configuration class in order to monitor all HTTP requests:

```php
class Filters extends BaseFilters
{

    /**
     * List of filter aliases that are always
     * applied before and after every request.
     *
     * @var array
     */
    public array $globals = [
        'before' => [
            ...,
            'inspector',
        ],
        'after' => [
           ...,
           'inspector',
        ],
    ];

}
```

{% hint style="info" %}
You can eventually turn off monitoring using the `ignoreRoutes` configuration property: <https://docs.inspector.dev/guides/codeigniter/configuration#ignore-routes>
{% endhint %}

### Verify And Deploy

Run the command below to check if your system is properly configured. If all checks are green you can deploy in your production environment.

```bash
php spark inspector:test
```

If everything is "green" you can release the update in your production environment.

By default Inspector will monitor:

* Http Requests
* Spark Commands
* Database queries
* Unhandled Exceptions

In the next sections you can learn how to further customize the library behaviour.

### Helper

**We highly recommend** adding the helper in the `Config/Autoload.php` configuration class to make it available globally into the application:

```php
class Autoload extends AutoloadConfig
{
    ...
    
    /**
     * -------------------------------------------------------------------
     * Helpers
     * -------------------------------------------------------------------
     * Prototype:
     *   $helpers = [
     *       'form',
     *   ];
     *
     * @var list<string>
     */
    public $helpers = ['inspector'];
}
```

The helper provides a shortcut to the inspector instance to monitor custom code blocks or manually report specific exceptions.

```php
// Load the helper if you haven't added it to Autoload.php
helper('inspector');

// Monitor custom code blocks
$json = inspector()->addSegment(function () {
    return file_get_contents('auth.json');
}, 'http', 'READ auth.json');

// Report an exception
inspector()->reportException(new \Exception("Whoops there's an error here."));
```

{% hint style="info" %}
Learn more about custom Segments here: <https://docs.inspector.dev/guides/raw-php/custom-segments>
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.inspector.dev/guides/codeigniter/installation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
