PHP code example of fromholdio / silverstripe-superlinker-redirection

1. Go to this page and download the library: Download fromholdio/silverstripe-superlinker-redirection 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/ */

    

fromholdio / silverstripe-superlinker-redirection example snippets


$redirector = RedirectionPage::create();
$redirector->Title = 'Old Product Page';
$redirector->URLSegment = 'old-product';
$redirector->write();

// Set redirect target
$redirector->RedirectLink()->LinkType = 'sitetree';
$redirector->RedirectLink()->SiteTreeID = $newPage->ID;
$redirector->RedirectLink()->write();

$redirector->publishRecursive();

$redirect = Redirection::create();
$redirect->FromURL = '/old-path';
$redirect->StatusCode = 301;
$redirect->write();

// Set redirect target
$redirect->RedirectLink()->LinkType = 'external';
$redirect->RedirectLink()->ExternalURL = 'https://example.com/new-path';
$redirect->RedirectLink()->write();

$redirector = RedirectionPage::create([
    'Title' => 'Old About Page',
    'URLSegment' => 'old-about'
]);
$redirector->write();

$redirector->RedirectLink()->LinkType = 'sitetree';
$redirector->RedirectLink()->SiteTreeID = $newAboutPage->ID;
$redirector->RedirectLink()->write();

$redirector->publishRecursive();

$redirector = RedirectionPage::create([
    'Title' => 'External Resource',
    'URLSegment' => 'external-resource'
]);
$redirector->write();

$redirector->RedirectLink()->LinkType = 'external';
$redirector->RedirectLink()->ExternalURL = 'https://external-site.com/resource';
$redirector->RedirectLink()->write();

$redirector->publishRecursive();

$redirector = RedirectionPage::create([
    'Title' => 'Download Brochure',
    'URLSegment' => 'brochure'
]);
$redirector->write();

$redirector->RedirectLink()->LinkType = 'file';
$redirector->RedirectLink()->FileID = $brochureFile->ID;
$redirector->RedirectLink()->write();

$redirector->publishRecursive();

$redirect = Redirection::create([
    'FromURL' => '/old-blog/2020/article',
    'StatusCode' => 301
]);
$redirect->write();

$redirect->RedirectLink()->LinkType = 'sitetree';
$redirect->RedirectLink()->SiteTreeID = $newArticlePage->ID;
$redirect->RedirectLink()->write();

use SilverStripe\CMS\Model\RedirectorPage as CoreRedirectorPage;
use Fromholdio\SuperLinkerRedirection\Pages\RedirectionPage;

// Get all core redirector pages
$oldRedirectors = CoreRedirectorPage::get();

foreach ($oldRedirectors as $old) {
    $new = RedirectionPage::create([
        'Title' => $old->Title,
        'URLSegment' => $old->URLSegment,
        'ParentID' => $old->ParentID
    ]);
    $new->write();

    // Migrate redirect target
    if ($old->RedirectionType === 'Internal') {
        $new->RedirectLink()->LinkType = 'sitetree';
        $new->RedirectLink()->SiteTreeID = $old->LinkToID;
    } else {
        $new->RedirectLink()->LinkType = 'external';
        $new->RedirectLink()->ExternalURL = $old->ExternalURL;
    }
    $new->RedirectLink()->write();

    $new->publishRecursive();
    $old->doArchive();
}