### Start a transaction on the master connection Source: https://www.yiiframework.com/doc/guide/2.0/en/db-dao Example of starting a transaction on the master connection and performing queries against the master. Both queries are executed on the master, and the transaction is committed or rolled back on exception. ```php $db = Yii::$app->db; // the transaction is started on the master connection $transaction = $db->beginTransaction(); try { // both queries are performed against the master $rows = $db->createCommand('SELECT * FROM user LIMIT 10')->queryAll(); $db->createCommand("UPDATE user SET username='demo' WHERE id=1")->execute(); $transaction->commit(); } catch(\Exception $e) { $transaction->rollBack(); throw $e; } catch(\Throwable $e) { $transaction->rollBack(); throw $e; } ``` -------------------------------- ### Widget translation setup Source: https://www.yiiframework.com/doc/guide/2.0/en/tutorial-i18n Shows a widget class that registers translations for its messages using a wildcard category and a fileMap, similar to the module example. ```php registerTranslations(); } public function registerTranslations() { $i18n = Yii::$app->i18n; $i18n->translations['widgets/menu/*'] = [ 'class' => 'yii\i18n\PhpMessageSource', 'sourceLanguage' => 'en-US', 'basePath' => '@app/widgets/menu/messages', 'fileMap' => [ 'widgets/menu/messages' => 'messages.php', ], ]; } public function run() { echo $this->render('index'); } public static function t($category, $message, $params = [], $language = null) { return Yii::t('widgets/menu/' . $category, $message, $params, $language); } } ``` -------------------------------- ### Run and get help for migrations Source: https://www.yiiframework.com/doc/guide/2.0/en/db-migrations Use these commands to run all pending migrations and to get help on the migrate command. ```bash php yii migrate php yii help migrate ``` -------------------------------- ### Install Composer on Linux and Mac OS X Source: https://www.yiiframework.com/doc/guide/2.0/en/start-installation Run these commands to install Composer globally on Linux or Mac OS X. The first downloads the installer, the second moves composer.phar to /usr/local/bin/composer. ```bash curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composer ``` -------------------------------- ### Start PHP built-in web server Source: https://www.yiiframework.com/doc/guide/2.0/en/test-environment-setup Starts the PHP built-in web server on localhost:8080 with the web directory as document root, required for acceptance tests. ```bash php -S localhost:8080 -t web ``` -------------------------------- ### Start built-in PHP server Source: https://www.yiiframework.com/doc/guide/2.0/en/start-installation Run this command in the project root to start the built-in PHP web server. By default it listens on port 8080. ```bash php yii serve ``` -------------------------------- ### Basic DI container get() usage with aliases and parameters Source: https://www.yiiframework.com/doc/guide/2.0/en/concept-di-container Shows how to use the container's get() method to retrieve an object by alias or class name, optionally passing constructor parameters and a configuration array. The comments in the code show the equivalent manual instantiation for each call. ```php // "db" is a previously registered alias name $db = $container->get('db'); // equivalent to: $engine = new \app\components\SearchEngine($apiKey, $apiSecret, ['type' => 1]); $engine = $container->get('app\components\SearchEngine', [$apiKey, $apiSecret], ['type' => 1]); // equivalent to: $api = new \app\components\Api($host, $apiKey); $api = $container->get('app\components\Api', ['host' => $host, 'apiKey' => $apiKey]); ``` -------------------------------- ### Start all services in background with docker-compose up -d Source: https://www.yiiframework.com/doc/guide/2.0/en/tutorial-docker Starts all services defined in your Docker Compose stack in the background. ```bash docker-compose up -d ``` -------------------------------- ### Examples of Url::toRoute() with various routes and schemes Source: https://www.yiiframework.com/doc/guide/2.0/en/helper-url Multiple examples of Url::toRoute() showing relative routes, aliases, and absolute URLs with scheme specification. The comments show the generated URLs. ```php // /index.php?r=site%2Findex echo Url::toRoute('site/index'); // /index.php?r=site%2Findex&src=ref1#name echo Url::toRoute(['site/index', 'src' => 'ref1', '#' => 'name']); // /index.php?r=post%2Fedit&id=100 assume the alias "@postEdit" is defined as "post/edit" echo Url::toRoute(['@postEdit', 'id' => 100]); // https://www.example.com/index.php?r=site%2Findex echo Url::toRoute('site/index', true); // https://www.example.com/index.php?r=site%2Findex echo Url::toRoute('site/index', 'https'); ``` -------------------------------- ### Install Codeception and modules via Composer Source: https://www.yiiframework.com/doc/guide/2.0/en/test-environment-setup Run this console command to install Codeception 5 and its modules as development dependencies. Requires PHP 8.0 or higher; for PHP 7.4, change the version constraint to ^4.0. ```bash composer require --dev 'codeception/codeception:^5.0' codeception/module-yii2 codeception/module-asserts codeception/module-filesystem codeception/module-phpbrowser codeception/verify ``` -------------------------------- ### GET query filter parameters Source: https://www.yiiframework.com/doc/guide/2.0/en/rest-filtering-collections Example of the same filtering conditions as a GET query string. Uses the 'filter' key with array syntax for 'in' and 'like' operators. ```http ?filter[id][in][]=2&filter[id][in][]=5&filter[id][in][]=9&filter[title][like]=cheese ``` -------------------------------- ### Configuration with properties, events, and behaviors Source: https://www.yiiframework.com/doc/guide/2.0/en/concept-configurations Example configuration that includes initial property values, an event handler for the 'search' event, and a behavior configuration for 'indexer'. The behavior configuration is itself a configuration array. ```php [ 'class' => 'app\components\SearchEngine', 'apiKey' => 'xxxxxxxx', 'on search' => function ($event) { Yii::info("Keyword searched: " . $event->keyword); }, 'as indexer' => [ 'class' => 'app\components\IndexerBehavior', // ... property init values ... ], ] ``` -------------------------------- ### Examples of Url::current() with parameter modification Source: https://www.yiiframework.com/doc/guide/2.0/en/helper-url Examples of Url::current() showing how to create URLs based on the current route and GET parameters, with options to remove or modify parameters. ```php // assume $_GET = ['id' => 123, 'src' => 'google'], current route is "post/view" // /index.php?r=post%2Fview&id=123&src=google echo Url::current(); // /index.php?r=post%2Fview&id=123 echo Url::current(['src' => null]); // /index.php?r=post%2Fview&id=100&src=google echo Url::current(['id' => 100]); ``` -------------------------------- ### Module initialization with configuration file Source: https://www.yiiframework.com/doc/guide/2.0/en/structure-modules Shows how to load module configuration from a separate config.php file using \Yii::configure() in the init() method. ```php public function init() { parent::init(); // initialize the module with the configuration loaded from config.php \Yii::configure($this, require __DIR__ . '/config.php'); } ``` ```php [ // list of component configurations ], 'params' => [ // list of parameters ], ]; ``` -------------------------------- ### Use findOne() and findAll() shortcuts Source: https://www.yiiframework.com/doc/guide/2.0/en/db-active-record Demonstrates the shortcut methods findOne() and findAll() with scalar, array, and associative array parameters. Includes SQL comments showing the generated queries. ```php // returns a single customer whose ID is 123 // SELECT * FROM `customer` WHERE `id` = 123 $customer = Customer::findOne(123); // returns customers whose ID is 100, 101, 123 or 124 // SELECT * FROM `customer` WHERE `id` IN (100, 101, 123, 124) $customers = Customer::findAll([100, 101, 123, 124]); // returns an active customer whose ID is 123 // SELECT * FROM `customer` WHERE `id` = 123 AND `status` = 1 $customer = Customer::findOne([ 'id' => 123, 'status' => Customer::STATUS_ACTIVE, ]); // returns all inactive customers // SELECT * FROM `customer` WHERE `status` = 0 $customers = Customer::findAll([ 'status' => Customer::STATUS_INACTIVE, ]); ``` -------------------------------- ### Implement migration to create news table Source: https://www.yiiframework.com/doc/guide/2.0/en/db-migrations This example shows how to implement the `up()` method to create a `news` table using abstract types, and the `down()` method to drop it. Use this when you need to define a reversible migration. ```php createTable('news', [ 'id' => Schema::TYPE_PK, 'title' => Schema::TYPE_STRING . ' NOT NULL', 'content' => Schema::TYPE_TEXT, ]); } public function down() { $this->dropTable('news'); } } ``` -------------------------------- ### URL to display static page via ViewAction Source: https://www.yiiframework.com/doc/guide/2.0/en/structure-views Example URL that triggers the ViewAction with the 'view' GET parameter set to 'about'. The action looks for the view under @app/views/site/pages. ```text http://localhost/index.php?r=site%2Fpage&view=about ``` -------------------------------- ### config.php for module configuration Source: https://www.yiiframework.com/doc/guide/2.0/en/structure-modules Example configuration file returning an array with components and params. Used with the init() method to configure the module or application. ```php [ 'response' => [ 'class' => 'yii\web\Response', 'format' => \yii\web\Response::FORMAT_JSON, 'charset' => 'UTF-8', ], ], 'params' => [ // list of parameters ], ]; ``` -------------------------------- ### count() query method example Source: https://www.yiiframework.com/doc/guide/2.0/en/db-query-builder Shows how to use count() to get the number of rows matching a condition. The generated SQL is SELECT COUNT(*) FROM `user` WHERE `last_name`=:last_name. ```php // executes SQL: SELECT COUNT(*) FROM `user` WHERE `last_name`=:last_name $count = (new \yii\db\Query()) ->from('user') ->where(['last_name' => 'Smith']) ->count(); ``` -------------------------------- ### Create project directory and change to it Source: https://www.yiiframework.com/doc/guide/2.0/en/tutorial-yii-as-micro-framework Commands to create a new directory for the micro-app and change into it. Unix-based commands are shown; similar commands exist on Windows. ```bash mkdir micro-app cd micro-app ``` -------------------------------- ### Create command to inspect SQL and parameters Source: https://www.yiiframework.com/doc/guide/2.0/en/db-query-builder Shows how to create a command from a query to examine the generated SQL and bound parameters, and then execute it to get all rows. ```php $command = (new \yii\db\Query()) ->select(['id', 'email']) ->from('user') ->where(['last_name' => 'Smith']) ->limit(10) ->createCommand(); // show the SQL statement echo $command->sql; // show the parameters to be bound print_r($command->params); // returns all rows of the query result $rows = $command->queryAll(); ``` -------------------------------- ### Get client host and IP address Source: https://www.yiiframework.com/doc/guide/2.0/en/runtime-requests Use Yii::$app->request->userHost and userIP to retrieve the client's host name and IP address. Works out of the box in a normal setup with a single webserver. ```php $userHost = Yii::$app->request->userHost; $userIP = Yii::$app->request->userIP; ``` -------------------------------- ### Embed images in email content Source: https://www.yiiframework.com/doc/guide/2.0/en/tutorial-mailing Use $message->embed() to embed an image and get an attachment id for use in an img tag. This example shows composing a message with a view file and passing the image file name. ```php Yii::$app->mailer->compose('embed-email', ['imageFileName' => '/path/to/image.jpg']) // ... ->send(); ``` -------------------------------- ### Retrieve a column of values with ArrayHelper::getColumn Source: https://www.yiiframework.com/doc/guide/2.0/en/helper-array Use ArrayHelper::getColumn to extract a column from an array of rows or objects. The example gets the 'id' column, resulting in ['123', '345']. For complex transformations, the second argument can be an anonymous function. ```php $array = [ ['id' => '123', 'data' => 'abc'], ['id' => '345', 'data' => 'def'], ]; $ids = ArrayHelper::getColumn($array, 'id'); ``` ```php $result = ArrayHelper::getColumn($array, function ($element) { return $element['id']; }); ``` -------------------------------- ### Check HTTP request method with Yii2 request properties Source: https://www.yiiframework.com/doc/guide/2.0/en/runtime-requests Use the request component's boolean properties to check if the current request is AJAX, GET, POST, or PUT. The example assigns the request object to a variable and then checks each property. ```php $request = Yii::$app->request; if ($request->isAjax) { /* the request is an AJAX request */ } if ($request->isGet) { /* the request method is GET */ } if ($request->isPost) { /* the request method is POST */ } if ($request->isPut) { /* the request method is PUT */ } ``` -------------------------------- ### URL examples for fields and expand parameters Source: https://www.yiiframework.com/doc/guide/2.0/en/rest-resources Shows how to use the fields and expand query parameters to control which fields are returned in RESTful API responses. ```http // returns all fields as declared in fields() http://localhost/users // only returns "id" and "email" fields, provided they are declared in fields() http://localhost/users?fields=id,email // returns all fields in fields() and field "profile" if it is in extraFields() http://localhost/users?expand=profile // returns all fields in fields() and "author" from post if // it is in extraFields() of post model http://localhost/comments?expand=post.author // only returns "id" and "email" provided they are in fields() and "profile" if it is in extraFields() http://localhost/users?fields=id,email&expand=profile ``` -------------------------------- ### Implement IdentityInterface in an Active Record class Source: https://www.yiiframework.com/doc/guide/2.0/en/security-authentication A complete example of a User class that extends ActiveRecord and implements IdentityInterface. It includes methods for finding identities by ID or access token, getting the ID, and validating auth keys. This is used for session-based or token-based authentication. ```php $token]); } /** * @return int|string current user ID */ public function getId() { return $this->id; } /** * @return string|null current user auth key */ public function getAuthKey() { return $this->auth_key; } /** * @param string $authKey * @return bool|null if auth key is valid for current user */ public function validateAuthKey($authKey) { return $this->getAuthKey() === $authKey; } } ``` -------------------------------- ### Install ZSH completion for Yii console Source: https://www.yiiframework.com/doc/guide/2.0/en/tutorial-console Create a directory for completions and download the ZSH completion script for Yii. ```bash mkdir -p ~/.zsh/completion curl -L https://raw.githubusercontent.com/yiisoft/yii2/master/contrib/completion/zsh/_yii -o ~/.zsh/completion/_yii ``` -------------------------------- ### Using asArray() to retrieve data as plain arrays Source: https://www.yiiframework.com/doc/guide/2.0/en/tutorial-performance-tuning Call asArray() on an Active Query to get results as plain arrays instead of Active Record objects, reducing memory usage when fetching large datasets. The example shows a PostController action that retrieves the first 100 posts and passes them to a view. ```php class PostController extends Controller { public function actionIndex() { $posts = Post::find()->limit(100)->asArray()->all(); return $this->render('index', ['posts' => $posts]); } } ``` -------------------------------- ### Initialize module with config.php (before change) Source: https://www.yiiframework.com/doc/guide/2.0/en/structure-modules Shows the original init() method that configures the module object using \Yii::configure($this, ...). This is the 'before' version in a user comment. ```php public function init() { parent::init(); // initialize the module with the configuration loaded from config.php \Yii::configure($this, require __DIR__ . '/config.php'); } ``` -------------------------------- ### Run composer install in a new container with docker-compose run --rm php composer install Source: https://www.yiiframework.com/doc/guide/2.0/en/tutorial-docker Runs composer installation in a new container. ```bash docker-compose run --rm php composer install ``` -------------------------------- ### get() Source: https://www.yiiframework.com/doc/guide/2.0/en/runtime-requests Retrieves GET request parameters from the request component. ```APIDOC ## get() ### Description Retrieves the value of a GET request parameter, or all GET parameters if no name is specified. This method is equivalent to accessing `$_GET` directly but is recommended for better testability. ### Method GET ### Parameters #### Query Parameters - **name** (string) - Optional - The name of the GET parameter to retrieve. If not specified, all GET parameters are returned. - **defaultValue** (mixed) - Optional - The default value to return if the parameter is not set. Defaults to `null`. ### Request Example ```php $request = Yii::$app->request; $get = $request->get(); // returns all GET parameters $id = $request->get('id'); // returns 'id' parameter or null $id = $request->get('id', 1); // returns 'id' parameter or 1 if not set ``` ### Response #### Success Response Returns the value of the specified GET parameter, or an array of all GET parameters if no name is given. #### Response Example ```php // Example: if $_GET = ['id' => 5, 'name' => 'John'] $get = $request->get(); // ['id' => 5, 'name' => 'John'] $id = $request->get('id'); // 5 $id = $request->get('id', 1); // 5 ``` ``` -------------------------------- ### Create and configure an object with Yii::createObject Source: https://www.yiiframework.com/doc/guide/2.0/en/intro-upgrade-from-v1 Demonstrates how to instantiate a configurable class using a configuration array and constructor parameters. The 'class' key specifies the class name, and property values are set after construction. ```php $object = Yii::createObject([ 'class' => 'MyClass', 'property1' => 'abc', 'property2' => 'cde', ], [$param1, $param2]); ``` -------------------------------- ### Composer.json replace block to prevent asset installation Source: https://www.yiiframework.com/doc/guide/2.0/en/start-installation Add these lines to your composer.json to prevent Composer from installing assets via Bower/NPM. You are then responsible for asset installation and version collision resolution. ```json "replace": { "bower-asset/jquery": ">=1.11.0", "bower-asset/inputmask": ">=3.2.0", "bower-asset/punycode": ">=1.3.0", "bower-asset/yii2-pjax": ">=2.0.0" }, ``` -------------------------------- ### Creating a component instance with configuration Source: https://www.yiiframework.com/doc/guide/2.0/en/concept-components Shows two ways to instantiate a component with configuration: directly with new and using Yii::createObject(). The latter is more powerful because it uses a dependency injection container. ```php $component = new MyClass(1, 2, ['prop1' => 3, 'prop2' => 4]); // alternatively $component = \Yii::createObject([ 'class' => MyClass::class, 'prop1' => 3, 'prop2' => 4, ], [1, 2]); ``` -------------------------------- ### Unique validator configuration example Source: https://www.yiiframework.com/doc/guide/2.0/en/tutorial-core-validators Example of using the 'unique' validator to check if the 'website' attribute is unique in the corresponding table column. The example shows a basic configuration without additional options. ```php ['website', 'unique'] ``` -------------------------------- ### Bootstrap property configuration example Source: https://www.yiiframework.com/doc/guide/2.0/en/structure-applications Shows the structure of the bootstrap property, which takes an array of extension specifications. Each extension is defined with name and version, and optionally a bootstrap element for bootstrapping class or configuration. ```php ``` ``` ``` -------------------------------- ### Access installed application Source: https://www.yiiframework.com/doc/guide/2.0/en/start-installation Use this URL in your browser to access the installed Yii application. ```text http://localhost:8080/ ``` -------------------------------- ### Add and remove CSS styles with Html::addCssStyle and Html::removeCssStyle Source: https://www.yiiframework.com/doc/guide/2.0/en/helper-html Demonstrates setting up CSS styles using the 'style' attribute. Shows adding a style string and removing properties by array. The comments show the resulting style attributes. ```php $options = ['style' => ['width' => '100px', 'height' => '100px']]; // gives style="width: 100px; height: 200px; position: absolute;" Html::addCssStyle($options, 'height: 200px; position: absolute;'); // gives style="position: absolute;" Html::removeCssStyle($options, ['width', 'height']); ``` -------------------------------- ### Create and Apply Database Migration Source: https://www.yiiframework.com/doc/guide/2.0/en/tutorial-yii-as-micro-framework Commands to create a migration file for the post table and apply it to the database. Requires a separate configuration file as explained in the tutorial. ```bash vendor/bin/yii migrate/create --appconfig=config.php create_post_table --fields="title:string,body:text" vendor/bin/yii migrate/up --appconfig=config.php ``` -------------------------------- ### Get GET and POST parameters via request component Source: https://www.yiiframework.com/doc/guide/2.0/en/runtime-requests Use the `request` component's `get()` and `post()` methods to retrieve request parameters. These methods provide a testable alternative to directly accessing `$_GET` and `$_POST`. ```php $request = Yii::$app->request; $get = $request->get(); // equivalent to: $get = $_GET; $id = $request->get('id'); // equivalent to: $id = isset($_GET['id']) ? $_GET['id'] : null; $id = $request->get('id', 1); // equivalent to: $id = isset($_GET['id']) ? $_GET['id'] : 1; $post = $request->post(); // equivalent to: $post = $_POST; $name = $request->post('name'); // equivalent to: $name = isset($_POST['name']) ? $_POST['name'] : null; $name = $request->post('name', ''); // equivalent to: $name = isset($_POST['name']) ? $_POST['name'] : ''; ``` -------------------------------- ### Implement RateLimitInterface in User model Source: https://www.yiiframework.com/doc/guide/2.0/en/rest-rate-limiting Shows the required methods getRateLimit, loadAllowance, and saveAllowance for the User model to enable rate limiting. The example uses a rate limit of one request per second and stores allowance and timestamp in model attributes. ```php public function getRateLimit($request, $action) { return [$this->rateLimit, 1]; // $rateLimit requests per second } public function loadAllowance($request, $action) { return [$this->allowance, $this->allowance_updated_at]; } public function saveAllowance($request, $action, $allowance, $timestamp) { $this->allowance = $allowance; $this->allowance_updated_at = $timestamp; $this->save(); } ``` -------------------------------- ### Install and configure fxp/composer-asset-plugin Source: https://www.yiiframework.com/doc/guide/2.0/en/structure-assets Install the fxp/composer-asset-plugin globally and configure installer paths in composer.json. This plugin allows managing Bower and NPM packages through Composer without changing application config. Note that it significantly slows down composer update compared to asset-packagist. ```bash composer global require "fxp/composer-asset-plugin:^1.4.1" ``` ```json "config": { "fxp-asset": { "installer-paths": { "npm-asset-library": "vendor/npm", "bower-asset-library": "vendor/bower" } } } ``` -------------------------------- ### Access theme object and get URL/path for resources Source: https://www.yiiframework.com/doc/guide/2.0/en/output-theming In a view file, $this->theme returns the theme object. Use getUrl() to get the base URL plus the given resource path, and getPath() to get the base path plus the resource path. ```php $theme = $this->theme; // returns: $theme->baseUrl . '/img/logo.gif' $url = $theme->getUrl('img/logo.gif'); // returns: $theme->basePath . '/img/logo.gif' $file = $theme->getPath('img/logo.gif'); ``` -------------------------------- ### Initialize module with config.php (after change) Source: https://www.yiiframework.com/doc/guide/2.0/en/structure-modules Shows the modified init() method that configures the application object using \Yii::configure(\Yii::$app, ...). This is the 'after' version in a user comment. ```php public function init() { parent::init(); // initialize the module with the configuration loaded from config.php \Yii::configure(\Yii::$app, require __DIR__ . '/config.php'); } ``` -------------------------------- ### Application configuration with bootstrap property Source: https://www.yiiframework.com/doc/guide/2.0/en/structure-application-components Shows how to configure the bootstrap property to load the 'log' component and use closures for custom bootstrapping. The closures receive the application instance and can return a component or just run code. ```php [ 'bootstrap' => [ 'log', function($app){ return new ComponentX(); }, function($app){ // some code return; } ], 'components' => [ 'log' => [ // configuration for "log" component ], ], ] ``` -------------------------------- ### Start a transaction on the slave connection Source: https://www.yiiframework.com/doc/guide/2.0/en/db-dao Explicitly start a transaction using the slave connection. By default, transactions use the master connection. ```php $transaction = Yii::$app->db->slave->beginTransaction(); ``` -------------------------------- ### Add new action with 'extraPatterns' Source: https://www.yiiframework.com/doc/guide/2.0/en/rest-routing Add a new endpoint 'GET /users/search' by configuring 'extraPatterns' to map the pattern 'GET search' to the action 'search'. ```php [ 'class' => 'yii\rest\UrlRule', 'controller' => 'user', 'extraPatterns' => [ 'GET search' => 'search', ], ] ``` -------------------------------- ### Accept header versioning examples Source: https://www.yiiframework.com/doc/guide/2.0/en/rest-versioning Shows two ways to specify API version via the Accept header: using a parameter or a vendor content type. ```http // via a parameter Accept: application/json; version=v1 // via a vendor content type Accept: application/vnd.company.myapp-v1+json ``` -------------------------------- ### Configuring bootstrap property with various component formats Source: https://www.yiiframework.com/doc/guide/2.0/en/structure-applications Shows how to specify components to run during bootstrapping, including component ID, class name, configuration array, and anonymous function. ```php [ 'bootstrap' => [ // an application component ID or module ID 'demo', // a class name 'app\components\Profiler', // a configuration array [ 'class' => 'app\components\Profiler', 'level' => 3, ], // an anonymous function function () { return new app\components\Profiler(); } ], ] ``` -------------------------------- ### Url::current() Source: https://www.yiiframework.com/doc/guide/2.0/en/helper-url Creates a URL based on the currently requested route and GET parameters. Allows modifying or removing GET parameters via the $params argument. ```APIDOC ## Url::current() ### Description Creates a URL based on the currently requested route and GET parameters. You can modify or remove some GET parameters or add new ones by passing a $params parameter. ### Method - **current($params = [])** - static method ### Parameters - **$params** (array) - Optional - An array of parameters to merge with the current GET parameters. To remove a parameter, set its value to null. To add or modify, set the value. ### Request Example ```php // assume $_GET = ['id' => 123, 'src' => 'google'], current route is "post/view" // /index.php?r=post%2Fview&id=123&src=google echo Url::current(); // /index.php?r=post%2Fview&id=123 echo Url::current(['src' => null]); // /index.php?r=post%2Fview&id=100&src=google echo Url::current(['id' => 100]); ``` ### Response Returns the generated URL string. ### Response Example ```php // /index.php?r=post%2Fview&id=123&src=google ``` ```