PHP code example of wellrested / redirect

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

    

wellrested / redirect example snippets




use WellRESTed\Redirect\RedirectHandler;
use WellRESTed\Message\Response;
use WellRESTed\Server;

$server = new Server();
$server->add($server->createRouter()
    ->register('GET', '/old-path', new RedirectHandler(301, '/new-path', new Response()))
);
$server->respond();



use Pimple\Container;
use Pimple\ServiceProviderInterface;
use WellRESTed\Redirect\RedirectHandler;
use WellRESTed\Server;

class ServiceProvider implements ServiceProviderInterface
{
    public function register(Container $c)
    {
        $c['redirect'] = $c->protect(
            function ($status, $location) {
                return new RedirectHandler($status, $location, new Response());
            }
        );

        $c['server'] = function ($c) {
            $server = new Server();
            $server->add($c['app:notFoundHandler']);
            $server->add($server->createRouter()
                ->register('GET', '/', [
                    $c['app:rootHandler']
                ])
                ->register('GET',  '/old-path', [
                    $c['redirect'](301, '/new-path')
                ])
                ->register('GET',  '/new-path', [
                    $$c['app:newThing']
                ])
                ->register('POST', '/login', [
                    $c['app:loginHandler'],
                    $c['redirect'](303, '/')
                ])
            );
            return $server;
        };
    }
}