# Route overrides

{% hint style="success" %}
This is the official documentation of the `forestadmin/laravel-forestadmin` v2+ and `forestadmin/symfony-forestadmin` PHP agents.
{% endhint %}

[Route overrides](https://docs.forestadmin.com/documentation/reference-guide/routes/override-a-route) allowed customizing the behavior of the routes exposed by the agent.

This very low-level feature was used to implement many use cases:

* Attach handlers to events in the UI
* Customize filtering, search and sort behaviors
* Other advanced use cases.

Because our new agent API is higher-level, the protocol used to communicate between the agent and the application can no longer be manipulated.

## Code cheatsheet

| What was the route override used for?   | How to migrate it?                                                                                                                                                                    |
| --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Add custom permissions                  | Use [.addHook()](/developer-guide-agents-php/agent-customization/hooks.md) and throw `forbidden errors`                                                                               |
| Add validation to fields                | Use [.addFieldValidation()](/developer-guide-agents-php/agent-customization/fields/validation.md) instead                                                                             |
| Add validation to whole records         | Use [.addHook()](/developer-guide-agents-php/agent-customization/hooks.md) and throw `validation errors`                                                                              |
| Run code on UI events                   | Use [.addHook()](/developer-guide-agents-php/agent-customization/hooks.md) to perform custom logic                                                                                    |
| Change the search behavior              | Use [.replaceSearch()](/developer-guide-agents-php/agent-customization/search.md) to implement your custom search logic                                                               |
| Change the filtering behavior of fields | Use [.replaceFieldOperator()](/developer-guide-agents-php/agent-customization/fields/filter.md)                                                                                       |
| Change the sort behavior of fields      | Use [.replaceFieldSorting()](/developer-guide-agents-php/agent-customization/fields/sort.md)                                                                                          |
| Other use case                          | If you are stuck or think that this guide can be improved, please [expose your use case in the community forums](https://community.forestadmin.com/) and we will be happy to help you |

## Examples

### Add custom permissions

{% hint style="warning" %}
Custom permissions would better be implemented by using the [Roles](https://docs.forestadmin.com/user-guide/project-settings/teams-and-users/manage-roles) feature from the UI.
{% endhint %}

{% tabs %}
{% tab title="Before" %}

* Define a new route into your `web.php` file:
* Add a new method into your Controller.

```php
<?php

namespace App\Http\Controllers;

use App\Models\Book;
use Illuminate\Http\Response;

class BooksController extends Controller
{
    public function destroy(int $id): Response
    {
        Book::where('id', $id)->delete();

        return response()->noContent();
    }
}
```

{% endtab %}

{% tab title="After" %}

```php
$forestAgent->customizeCollection('Book', function (CollectionCustomizer $builder) {
    $builder->addHook('Before', 'Delete', function ($context) {
        $isAllowed = true; // YOUR LOGIC HERE

        if (!$isAllowed) {
            $context->throwForbiddenError('This collection is protected, you cannot remove from it.');
        }
    });
})
```

{% endtab %}
{% endtabs %}


---

# 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.forestadmin.com/developer-guide-agents-php/getting-started/migrating/code-transformations/route-overrides.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.
