Dynamic Redirects

The redirects component is a very powerful functionality that allows you to visually manage your request redirects.

In order to provide you with a complex crud functionality inside the admin, the redirects crud implements the following out of the box:

Admin Interface

Before going deeper, you should know that there's already a section in the admin from where you can manage all your settings.

You can find the redirects section inside Admin -> System Settings -> Redirects.
Feel free to explore all available options this section offers.

Redirects List

How It Works

First of all, let's understand the workflow behind redirects.

The Workflow

Redirect Via Middleware

Once you have your redirects setup inside the admin, it's time to make the application aware of them and actually redirect incoming requests when needed.

Attach Middleware

Inside your app/Http/Kernel.php file, add the Varbox\Middleware\RedirectRequests middleware to your entire web middleware group.

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        ...
        \Varbox\Middleware\RedirectRequests::class
    ],

    ...
];

Redirect Via PHP File

If you don't like the middleware approach because for example it creates between one and two queries every request, you can still leverage the redirect capability, by hooking into the public/index.php file.

Export Redirects To File

First of all, inside the "Admin -> System Settings -> Redirects", click on the "Export File" button.

This will create the bootstrap/redirects.php file containing all you redirects in an array format.

If you're trying to "Export File" an empty redirect list, the actual bootstrap/redirects.php will be deleted, as it won't be any need for it.

Add Redirection Code

Once you've exported your redirects, go inside the public/index.php file and at the very beginning add:

if (file_exists(__DIR__ . '/../bootstrap/redirects.php')) {
    foreach (require_once __DIR__ . '/../bootstrap/redirects.php' as $redirect) {
        if ($_SERVER['REQUEST_URI'] == '/' . trim($redirect['from'], '/')) {
            header('Location: ' . '/' . trim($redirect['to'], '/'), true, $redirect['status']);
            die;
        }
    }
}

This code fragment will handle your redirects before even hitting the Laravel application.

Automate The Process

To automate the process of exporting the redirects every time a redirect is created, updated or deleted, set the automatic_export key to true inside the config/varbox/redirect.php config file.

/*
|
| Export the redirects into the "bootstrap/redirects.php" on every create / update / delete.
|
*/
'automatic_export' => true

Configuration

The redirect configuration file is located at config/varbox/redirect.php.

For more information on how you can customize the system settings components, please read the comments from their configuration files.

Overwrite Bindings

In your projects, you may stumble upon the need to modify the behavior of these classes, in order to fit your needs. Varbox makes this possible via the config/varbox/bindings.php configuration file. In that file, you'll find every customizable class the platform uses.

For more information on how the class binding works, please refer to the Custom Bindings documentation section.

The redirect classes available for binding overwrites are:

Varbox\Models\Redirect

Found in config/varbox/bindings.php at models.redirect_model key.
This class represents the redirect model.

Varbox\Controllers\RedirectsController

Found in config/varbox/bindings.php at controllers.redirects_controller key.
This class is used for interactions with the "Admin -> System Settings -> Redirects" section.

Varbox\Requests\RedirectRequest

Found in config/varbox/bindings.php at form_requests.redirect_form_request key.
This class is used for validating any redirect when creating or updating.

Varbox\Middleware\RedirectRequests

Found in config/varbox/bindings.php at middleware.redirect_requests_middleware key.
This middleware can be used by you to manage your redirects at application level.

Varbox\Filters\RedirectFilter

Found in config/varbox/bindings.php at filters.redirect_filter key.
This class is used for applying the filtering logic.

Varbox\Sorts\RedirectSort

Found in config/varbox/bindings.php at sorts.redirect_sort key.
This class is used for applying the sorting logic.