PHP code example of it-blaster / redirect-bundle

1. Go to this page and download the library: Download it-blaster/redirect-bundle 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/ */

    

it-blaster / redirect-bundle example snippets

 bash
$ php composer.phar update it-blaster/redirect-bundle
 php

// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new ItBlaster\RedirectBundle\ItBlasterRedirectBundle(),
    );
}
 php
    public function showExceptionAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null)
    {
        $redirect = $this->oldRedirect($request, $exception);
        if ($redirect) {
            return $this->redirect($redirect);
        }
        ...
    }

    /**
     * Если ссылка пользователя ведёт на старый сайт, редиректим на новый сайт
     *
     * @param Request $request
     * @param FlattenException $exception
     */
    protected function oldRedirect(Request $request, FlattenException $exception)
    {
        $redirect_object = RedirectQuery::create()
            ->filterByOldUrl('%'.$request->getUri().'%', \Criteria::LIKE)
            ->findOne();
        if ($redirect_object) { //ща средиректим
            return $redirect_object->getNewUrl(); //адрес на указанную ссылку
        }
    }
 php
    protected function oldRedirect(Request $request, FlattenException $exception)
    {
        $redirect_object = RedirectQuery::create()
            ->filterByOldUrl('%'.$request->getUri().'%', \Criteria::LIKE)
            ->findOne();
        if ($redirect_object) { //ща средиректим
            if ($redirect_object->getNewUrl()) {
                return $redirect_object->getNewUrl(); //адрес на указанную ссылку
            } else {
                $model = $redirect_object->getModel();
                $object_id = $redirect_object->getObjectId();
                if ($model && $object_id) {
                    $route_name = false;
                    $route_params = array();
                    switch ($model) {
                        case 'news_i18n':
                            $object = NewsQuery::create()->findOneById($object_id);
                            if ($object) {
                                $route_name = 'news-item';
                                $route_params = array('alias'=>$object->getAlias());
                            }
                            break;
                        ...
                    }
                    if ($route_name) {
                        return $this->generateUrl($route_name, $route_params); //адрес на объект
                    }
                }
            }
        }
        return false;
    }