PHP code example of bnomei / kirby3-redirects

1. Go to this page and download the library: Download bnomei/kirby3-redirects library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bnomei / kirby3-redirects example snippets


 
return [
    'hooks' => [
        'redirect:before' => function (int $code, \Bnomei\Redirect $redirect) {
            // do whatever you need, like...
            monolog('redirect')->info($code, [
                'from' => $redirect->from(), 
                'to' => $redirect->to()
            ]);
        },
        '404:before' => function (\Kirby\Http\Route $route, string $path, string $method) {
            // do whatever you need, like...
            F::write(kirby()->root('logs').'/404.log', implode(' ', [
                '['.date('Y-m-d H:i:s').']',
                '404.ERROR',
                $method,
                $path,
                PHP_EOL,
            ]), true);
        },
    ],
    // other config...
];

// add single item
$success = site()->appendRedirects(
    ['fromuri' => '/posts?id=1', 'touri' => '/blog/1', 'code' => 301]
);

// add multiple items with nested array
$success = site()->appendRedirects([
    ['fromuri' => '/posts?id=2', 'touri' => '/blog/2', 'code' => 301],
    // ...
    ['fromuri' => '/posts?id=999', 'touri' => '/blog/999', 'code' => 301],
]);

// remove single item
$success = site()->removeRedirects(
    ['fromuri' => '/posts?id=1', 'touri' => '/blog/1']
);

// remove multiple items with nested array
$success = site()->removeRedirects([
    ['fromuri' => '/posts?id=3', 'touri' => '/blog/3'],
    ['fromuri' => '/posts?id=5', 'touri' => '/blog/5'],
    ['fromuri' => '/posts?id=7', 'touri' => '/blog/7'],
]);

return [
  'cache' => [
    'driver' => 'apcu', // or redis
  ],
  'bnomei.redirects.cache' => [
    'type' => 'apcu', // or redis
  ],
];