mspirkov / yii2-web
Yii2 Web extension.
Installs: 4
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/mspirkov/yii2-web
Requires
- php: >=7.4
- yiisoft/yii2: ^2.0.53
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.89
- phpstan/phpstan: ^2.1
- phpstan/phpstan-phpunit: ^2.0
- phpunit/phpunit: ^9.6
- rector/rector: ^2.2
README
A package of helper classes for working with web components in Yii2.
Installation
Run
php composer.phar require mspirkov/yii2-web
or add
"mspirkov/yii2-web": "^0.1"
to the require section of your composer.json file.
Components
Request
A wrapper for \yii\web\Request for easier handling of GET and POST parameters.
It contains the following methods:
getGetInt- gets the value of a GET parameter by its name and tries to convert it to an integer.getGetFloat- gets the value of the GET parameter by its name and tries to convert it to a floating-point number.getGetBool- gets the value of the GET parameter by its name and tries to convert it to a boolean.getGetString- gets the value of the GET parameter by its name and tries to convert it to a string.getGetArray- gets the value of the GET parameter by its name and tries to convert it to an array.getPostInt- gets the value of a POST parameter by its name and tries to convert it to an integer.getPostFloat- gets the value of the POST parameter by its name and tries to convert it to a floating-point number.getPostBool- gets the value of the POST parameter by its name and tries to convert it to a boolean.getPostString- gets the value of the POST parameter by its name and tries to convert it to a string.getPostArray- gets the value of the POST parameter by its name and checks that the value is an array.
Configuration
First, you need to replace the request component in the configuration:
<?php use MSpirkov\Yii2\Web\Request; return [ ... 'components' => [ 'request' => [ 'class' => Request::class, ... ], ... ], ];
IDE Autocomplete (Optional)
You also need to specify this class in __autocomplete.php so that the IDE knows which class to use:
<?php use yii\BaseYii; use yii\web\Application; use MSpirkov\Yii2\Web\Request; class Yii extends BaseYii { /** @var __Application */ public static $app; } /** * @property-read Request $request */ class __Application extends Application {}
Basic Controller (Optional)
I also recommend that you create your own basic controller and specify Request there:
use MSpirkov\Yii2\Web\Request; /** * @property Request $request */ class Controller extends \yii\web\Controller { public function init(): void { parent::init(); $this->request = Instance::ensure($this->request, Request::class); } }
Usage example:
class ProductController extends Controller { /** * @return array{success: bool, message?: string} */ public function actionDelete(): array { $this->response->format = Response::FORMAT_JSON; if (!$this->request->isAjax) { throw new BadRequestHttpException(); } return $this->service->delete($this->request->getPostInt('id')); } }
CookieManager
A utility class for managing cookies.
This class encapsulates the logic for adding, removing, checking existence, and retrieving cookies, using the \yii\web\Request
and \yii\web\Response objects. It simplifies working with cookies by abstracting implementation details and providing more
convenient methods.
It contains the following methods:
has- checks if a cookie with the specified name exists.get- returns the cookie with the specified name.add- adds a cookie to the response.remove- removes a cookie.removeAll- removes all cookies.
Usage example:
class CookieManager extends \MSpirkov\Yii2\Web\CookieManager { public function __construct() { parent::__construct( Instance::ensure('request', Request::class), Instance::ensure('response', Response::class), ); } }
class ExampleService { public function __construct( private readonly CookieManager $cookieManager, ) {} public function doSomething(): void { $this->cookieManager->add([ 'name' => 'someCookieName', 'value' => 'someCookieValue', ]); } }