1. Go to this page and download the library: Download yiisoft/yii-middleware 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/ */
yiisoft / yii-middleware example snippets
use Yiisoft\Yii\Middleware\ForceSecureConnection;
/**
* @var Psr\Http\Message\ResponseFactoryInterface $responseFactory
* @var Psr\Http\Message\ServerRequestInterface $request
* @var Psr\Http\Server\RequestHandlerInterface $handler
*/
$middleware = new ForceSecureConnection($responseFactory);
// Enables redirection from HTTP to HTTPS:
$middleware = $middleware->withRedirection(301);
// Disables redirection from HTTP to HTTPS:
$middleware = $middleware->withoutRedirection();
$response = $middleware->process($request, $handler);
$middleware = $middleware->withCSP('upgrade-insecure-requests; default-src https:');
// Or without the `Content-Security-Policy` header in response:
$middleware = $middleware->withoutCSP();
$maxAge = 3600; // Default is 31_536_000 (12 months).
$subDomains = false; // Whether to add the `ecurity` header in response:
$middleware = $middleware->withoutHSTS();
use Yiisoft\Yii\Middleware\HttpCache;
/**
* @var Psr\Http\Message\ServerRequestInterface $request
* @var Psr\Http\Server\RequestHandlerInterface $handler
*/
$middleware = new HttpCache();
// Specify callable that generates the last modified:
$middleware = $middleware->withLastModified(function (ServerRequestInterface $request, mixed $params): int {
$defaultLastModified = 3600;
// Some actions.
return $defaultLastModified;
});
// Specify callable that generates the ETag seed string:
$middleware = $middleware->withEtagSeed(function (ServerRequestInterface $request, mixed $params): string {
$defaultEtagSeed = '33a64df551425fcc55e4d42a148795d9f25f89d4';
// Some actions.
return $defaultEtagSeed;
});
$response = $middleware->process($request, $handler);
// Extra parameters for ETag seed string generation:
$middleware = $middleware->withParams(['parameter' => 'value']);
// The value of the `Cache-Control` HTTP header:
$middleware = $middleware->withCacheControlHeader('public, max-age=31536000');
// Default is `public, max-age=3600`. If null, the header won't be sent.
// Enable weak ETags generation (disabled by default):
$middleware = $middleware->withWeakTag();
// You should use weak ETags if the content is semantically equal, but not byte-equal.
use Yiisoft\Yii\Middleware\IpFilter;
/**
* @var Psr\Http\Message\ResponseFactoryInterface $responseFactory
* @var Psr\Http\Message\ServerRequestInterface $request
* @var Psr\Http\Server\RequestHandlerInterface $handler
* @var Yiisoft\Validator\Rule\ValidatorInterface $validator
*/
// Name of the request attribute holding client IP:
$clientIpAttribute = 'client-ip';
// If there is no such attribute, or it has no value, then the middleware will respond with 403 forbidden.
// If the name of the request attribute is `null`, then `REMOTE_ADDR` server parameter is used to determine client IP.
$middleware = new IpFilter($validator, $responseFactory, $clientIpAttribute);
// Change client IP validator:
$middleware = $middleware->withValidator($validator);
$response = $middleware->process($request, $handler);
use Yiisoft\Yii\Middleware\Redirect;
/**
* @var Psr\Http\Message\ResponseFactoryInterface $responseFactory
* @var Psr\Http\Message\ServerRequestInterface $request
* @var Psr\Http\Server\RequestHandlerInterface $handler
* @var Yiisoft\Router\UrlGeneratorInterface $urlGenerator
*/
$middleware = new Redirect($ipValidator, $urlGenerator);
// Specify URL for redirection:
$middleware = $middleware->toUrl('/login');
// Or specify route data for redirection:
$middleware = $middleware->toRoute('auth/login', ['parameter' => 'value']);
// If you have set a redirect URL with "toUrl()" method, the middleware ignores the route data, since the URL is a
// priority.
$response = $middleware->process($request, $handler);
// For permanent redirection (301):
$middleware = $middleware->permanent();
// For temporary redirection (302):
$middleware = $middleware->permanent();
// Or specify the status code yourself:
$middleware = $middleware->withStatus(303);
use Yiisoft\Yii\Middleware\Subfolder;
/**
* @var Psr\Http\Message\ServerRequestInterface $request
* @var Psr\Http\Server\RequestHandlerInterface $handler
* @var Yiisoft\Aliases\Aliases $aliases
* @var Yiisoft\Router\UrlGeneratorInterface $urlGenerator
*/
// URI prefix the specified immediately after the domain part (default is `null`):
$prefix = '/blog';
// The prefix value usually begins with a slash and must not end with a slash.
$middleware = new Subfolder($urlGenerator, $aliases, $prefix);
$response = $middleware->process($request, $handler);
use Yiisoft\Yii\Middleware\TagRequest;
/**
* @var Psr\Http\Message\ServerRequestInterface $request
* @var Psr\Http\Server\RequestHandlerInterface $handler
*/
$middleware = new TagRequest();
// In the process, a request attribute with the name `requestTag`
// and the generated value by the function `uniqid()` will be added.
$response = $middleware->process($request, $handler);
use Yiisoft\Yii\Middleware\Locale;
// Available locales.
$locales = ['en' => 'en-US', 'ru' => 'ru-RU', 'uz' => 'uz-UZ']
/**
* Specify supported locales.
*
* @var Locale $middleware
*/
$middleware = $middleware->withSupportedLocales($locales);
// Ignore requests which URLs that match "/api**" wildcard pattern.
$middleware = $middleware->withIgnoredRequestUrlPatterns(['/api**']);
$response = $middleware->process($request);
use Yiisoft\Yii\Middleware\Locale;
/** @var Locale $middleware */
$middleware = $middleware
->withCookieDuration(new DateInterval('P30D')) // Key parameter for activating saving to cookies.
// Extra customization.
->withCookieName('_custom_name')
->withSecureCookie(true)
use Yiisoft\Translator\TranslatorInterface;
use Yiisoft\Yii\Middleware\Event\SetLocaleEvent;
final class SetLocaleEventHandler
{
public function __construct(
private TranslatorInterface $translator
) {
}
public function handle(SetLocaleEvent $event): void
{
$this->translator->setLocale($event->getLocale());
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.