PHPackages                             locustv2/yii2-linkable-behavior - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. locustv2/yii2-linkable-behavior

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

locustv2/yii2-linkable-behavior
===============================

Makes it easier to create urls and links.

1.0.0(9y ago)11321BSD-3-ClausePHP

Since Mar 31Pushed 9y ago1 watchersCompare

[ Source](https://github.com/Locustv2/yii2-linkable-behavior)[ Packagist](https://packagist.org/packages/locustv2/yii2-linkable-behavior)[ Docs](https://github.com/locustv2/)[ RSS](/packages/locustv2-yii2-linkable-behavior/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

Linkable Behavior for Yii2 Components
=====================================

[](#linkable-behavior-for-yii2-components)

This extension help creating urls easier in yii2. This behavior provides support for components that have a page to display its contents. The page can be an action in a Module or simply in a Controller. It will be easier to get links related to this record without having to write Url Route over and over again.

[![Latest Stable Version](https://camo.githubusercontent.com/97bdd2e3a7844167cf478f3eb93a9ea3b20fcf4bb85f7c847611edf575d00fad/68747470733a2f2f706f7365722e707567782e6f72672f6c6f6375737476322f796969322d6c696e6b61626c652d6265686176696f722f762f737461626c65)](https://packagist.org/packages/locustv2/yii2-linkable-behavior)[![Total Downloads](https://camo.githubusercontent.com/f4281feedacb8fc99a637e53e6baf40852d0b168a4680136bb05545b394f5cf2/68747470733a2f2f706f7365722e707567782e6f72672f6c6f6375737476322f796969322d6c696e6b61626c652d6265686176696f722f646f776e6c6f616473)](https://packagist.org/packages/locustv2/yii2-linkable-behavior)[![Latest Unstable Version](https://camo.githubusercontent.com/67e9a02fa662d762c3b58ae0d264bf566f55508f86eedbee1739e2d8bebe1acd/68747470733a2f2f706f7365722e707567782e6f72672f6c6f6375737476322f796969322d6c696e6b61626c652d6265686176696f722f762f756e737461626c65)](https://packagist.org/packages/locustv2/yii2-linkable-behavior)[![License](https://camo.githubusercontent.com/4c83cfce50d15445eaa7fe56d19c1522ee75e0f25d1ebbd813d29a1ed732d258/68747470733a2f2f706f7365722e707567782e6f72672f6c6f6375737476322f796969322d6c696e6b61626c652d6265686176696f722f6c6963656e7365)](https://packagist.org/packages/locustv2/yii2-linkable-behavior)

Installation
------------

[](#installation)

The preferred way to install the library is through [composer](https://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist locustv2/yii2-linkable-behavior

```

or add

```
{
    "require": {
        "locustv2/yii2-linkable-behavior": "~1.0.0"
    }
}
```

to your `composer.json` file.

Usage
-----

[](#usage)

Add the behavior to your ActiveRecord that can he hotlinked:

```
namespace app\models;

use yii\db\ActiveRecord;
use locustv2\behaviors\LinkableBehavior;

class User extends ActiveRecord
{
  //...

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => LinkableBehavior::className(),
                'route' => '/user',
                'defaultAction' => 'view',
                'hotlinkTextAttr' => 'username',
                'defaultParams' => function ($record) {
                    return [
                        'id' => $record->id,
                    ];
                },
            ]
        ]);
    }
}
```

```
namespace app\models;

use yii\db\ActiveRecord;
use locustv2\behaviors\LinkableBehavior;

class Photo extends ActiveRecord
{
  //...

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => LinkableBehavior::className(),
                'route' => '/photo',
                'defaultAction' => 'view',
                'linkableParams' => function ($record) {
                    return [
                        'photoid' => $record->id,
                    ];
                },
                'useAbsoluteUrl' => true,
                'defaultParams' => function ($record) {
                    return [
                        'id' => $record->id,
                        'slug' => $record->slug
                    ];
                },
            ]
        ]);
    }
}
```

With that code in place, you can now use 4 available methods in your `User` and `Photo` ActiveRecord:

- `getUrlRoute($action = null, array $params = [])`
- `getUrlRouteTo(Component $component, $action = null)`
- `getHotlink($action = null, array $params = [], array $options = [])`
- `getHotlinkTo(Component $component, $action = null, array $params = [], array $options = [])`

### Examples (assuming that you use [pretty urls](http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html#using-pretty-urls))

[](#examples-assuming-that-you-use-pretty-urls)

#### `getUrlRoute($action = null, array $params = [])`

[](#geturlrouteaction--null-array-params--)

```
use yii\helpers\Url;
use app\models\User;

$user = User::findOne(['id' => 123]);

// /user/view?id=123
echo Url::to($user->urlRoute);

// /user/update?id=123
echo Url::to($user->getUrlRoute('update'));

// http://www.yoursite.com/user/profile?id=123&ref=facebook
echo Url::to($user->getUrlRoute('profile', ['ref' => 'facebook']), true);
```

#### `getUrlRouteTo(Component $component, $action = null)`

[](#geturlroutetocomponent-component-action--null)

```
use yii\helpers\Url;
use app\models\User;

$user = User::findOne(['id' => 123]);
$photo = $user->getPhotos()->one();

// /user/photo/view?id=123&photoid=456&slug=my-first-photo
echo Url::to($user->getUrlRouteTo($photo));

// /photo/user/view?id=456&slug=my-first-photo&uid=123
echo Url::to($photo->getUrlRouteTo($user));

// /user/photo/update?id=123&photoid=456&slug=my-first-photo
echo Url::to($user->getUrlRouteTo($photo, 'update'));
```

#### `getHotlink($action = null, array $params = [], array $options = [])`

[](#gethotlinkaction--null-array-params---array-options--)

```
use yii\helpers\Url;
use app\models\User;

$user = User::findOne(['id' => 123]);

// Locustv2
echo $user->hotLink;

// Locustv2
echo $user->getHotlink('update');

// Locustv2
echo $user->getHotlink('profile', ['ref' => 'facebook'], ['class' => 'text-bold']);
```

If you want to use absolute urls, you should set `LinkableBehavior::$useAbsoluteUrl` to `true`. If you want to disable hotlinks, you should set `LinkableBehavior::$disableHotlink` to `true`. `` will be used instead of ``

#### `getHotlinkTo(Component $component, $action = null, array $params = [], array $options = [])`

[](#gethotlinktocomponent-component-action--null-array-params---array-options--)

```
use yii\helpers\Url;
use app\models\User;

$user = User::findOne(['id' => 123]);
$photo = $user->getPhotos()->one();

// Locustv2
echo $user->getHotlinkTo($photo);

// http://www.yoursite.com/photo/user/view?id=456&slug=my-first-photo&uid=123
echo $photo->getHotlinkTo($user);

// Locustv2
echo Url::to($user->getHotlinkTo($photo, 'update', ['ref' => homepage], ['class' => 'font-bold']));
```

To do
-----

[](#to-do)

- Add unit tests

Contributing
------------

[](#contributing)

Feel free to send pull requests.

License
-------

[](#license)

For license information check the [LICENSE](LICENSE.md)-file.

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 90% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3378d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/43f3d13b7b517fc44662d62165016bfeacf0a5858a1cdc7f5c3a8423042bc40b?d=identicon)[Locustv2](/maintainers/Locustv2)

---

Top Contributors

[![Locustv2](https://avatars.githubusercontent.com/u/4509909?v=4)](https://github.com/Locustv2 "Locustv2 (9 commits)")[![pikseli](https://avatars.githubusercontent.com/u/2874851?v=4)](https://github.com/pikseli "pikseli (1 commits)")

---

Tags

behaviorlinkableurlsyii2yii2-behaviorsyii2-extensionurllinkyii2Behaviorlinkable

### Embed Badge

![Health badge](/badges/locustv2-yii2-linkable-behavior/health.svg)

```
[![Health](https://phpackages.com/badges/locustv2-yii2-linkable-behavior/health.svg)](https://phpackages.com/packages/locustv2-yii2-linkable-behavior)
```

###  Alternatives

[mdmsoft/yii2-autonumber

Auto number extension for the Yii framework

1831.2k](/packages/mdmsoft-yii2-autonumber)[baibaratsky/yii2-serialized-attributes-behavior

Yii2 Serialized Attributes Behavior

1175.1k9](/packages/baibaratsky-yii2-serialized-attributes-behavior)[asinfotrack/yii2-audittrail

Yii2-audittrail is a behavior and a set of widgets to track all modifications performed on a model

1627.3k](/packages/asinfotrack-yii2-audittrail)[valentinek/yii2-closure-table-behavior

This extension allows you to get functional for closure table trees.

1615.4k](/packages/valentinek-yii2-closure-table-behavior)[v0lume/yii2-meta-tags

DB based model meta data for SEO

194.1k](/packages/v0lume-yii2-meta-tags)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
