PHPackages                             carono/yii2-url-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. carono/yii2-url-behavior

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

carono/yii2-url-behavior
========================

Yii2 behavior and URL rule component for dynamic URL generation based on model, action, user roles, and application context.

1.0.2(7mo ago)011MITPHPPHP ^7.4 || 8.\*

Since Sep 15Pushed 7mo agoCompare

[ Source](https://github.com/carono/carono-yii2-url-behavior)[ Packagist](https://packagist.org/packages/carono/yii2-url-behavior)[ RSS](/packages/carono-yii2-url-behavior/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (4)Used By (0)

Yii2 URL Behavior
=================

[](#yii2-url-behavior)

A flexible behavior for Yii2 that provides dynamic URL generation based on user roles, application context, and custom rules.

Features
--------

[](#features)

- Role-based URL generation
- Multi-application support
- Flexible rule configuration
- Caching support for performance optimization
- Customizable URL rule parameters

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

[](#installation)

Add the package to your `composer.json`:

```
{
    "require": {
        "carono/yii2-url-behavior": "*"
    }
}
```

Usage
-----

[](#usage)

### Attaching the Behavior

[](#attaching-the-behavior)

Attach the behavior to your ActiveRecord model:

```
use carono\yii2\behaviors\UrlBehavior;

class Post extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            [
                'class' => UrlBehavior::class,
                'rules' => [
                    ['view', 'url' => ['post/view', 'id' => 'id']],
                    ['update', 'url' => ['post/update', 'id' => 'id'], 'role' => 'admin'],
                    ['delete', 'url' => ['post/delete', 'id' => 'id'], 'role' => ['admin', 'moderator']],
                ],
                'defaultUrl' => ['site/index'],
                'functionAlias' => 'getUrl' // Default method name
            ]
        ];
    }
}
```

### Configuration Options

[](#configuration-options)

- `rules`: Array of URL rules or a string method name that returns rules
- `defaultUrl`: Default URL when no rules match
- `functionAlias`: Method name to expose (default: `getUrl`)
- `authManager`: Auth manager component ID (default: `authManager`)
- `ruleClass`: Custom rule class implementation

### Rule Configuration

[](#rule-configuration)

Each rule can have these properties:

- `action`: Action name (required)
- `url`: URL pattern or callable (required)
- `role`: Role or array of roles (optional)
- `application`: Application ID restriction (optional)
- `params`: Additional parameters (optional)

### URL Generation

[](#url-generation)

```
$post = Post::findOne(1);

// Get URL as array
$urlArray = $post->getUrl('view');

// Get absolute URL string
$urlString = $post->getUrl('view', true);
```

### Advanced Rule Examples

[](#advanced-rule-examples)

```
'rules' => [
    [
        'view',
        'url' => ['post/view', 'id' => 'id'],
        'role' => 'user',
        'application' => 'frontend'
    ],
    [
        'update',
        'url' => function($model) {
            return ['post/update', 'id' => $model->id, 'slug' => $model->slug];
        },
        'role' => ['admin', 'editor']
    ],
    [
        'delete',
        'url' => ['post/delete'],
        'params' => [
            'id' => 'id',
            'timestamp' => function($model) {
                return time();
            }
        ]
    ]
]
```

Rule Class Reference
--------------------

[](#rule-class-reference)

The `UrlRule` class provides these properties:

- `action`: Target action name
- `url`: URL pattern or callable
- `role`: Role(s) required
- `application`: Application restriction
- `params`: Parameter configuration
- `authManager`: Auth manager component
- `cache`: Cache component for role caching
- `model`: Related model instance

Caching
-------

[](#caching)

The behavior supports role caching to improve performance:

```
'rules' => [
    ['view', 'url' => ['post/view'], 'role' => 'user', 'duration' => 3600]
]
```

Advanced Usage
--------------

[](#advanced-usage)

### Custom Rule Class

[](#custom-rule-class)

Create custom rule class by extending `UrlRule`:

```
class CustomUrlRule extends UrlRule
{
    public function compare($action, $user)
    {
        // Custom logic here
        return parent::compare($action, $user);
    }
}
```

### Dynamic Rules

[](#dynamic-rules)

Define rules using a model method:

```
public function behaviors()
{
    return [
        [
            'class' => UrlBehavior::class,
            'rules' => 'getUrlRules'
        ]
    ];
}

public function getUrlRules()
{
    return [
        ['view', 'url' => ['post/view', 'id' => 'id']],
        // ... more rules
    ];
}
```

License
-------

[](#license)

MIT

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance62

Regular maintenance activity

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% 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

Every ~0 days

Total

3

Last Release

238d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6193985?v=4)[Александр Касьянов](/maintainers/carono)[@carono](https://github.com/carono)

---

Top Contributors

[![carono](https://avatars.githubusercontent.com/u/6193985?v=4)](https://github.com/carono "carono (4 commits)")

---

Tags

yii2extension

### Embed Badge

![Health badge](/badges/carono-yii2-url-behavior/health.svg)

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

###  Alternatives

[vyants/yii2-daemon

Extension provides functionality for simple daemons creation and control

7859.0k](/packages/vyants-yii2-daemon)[dmstr/yii2-cookie-consent

Yii2 Cookie Consent Widget

1452.6k](/packages/dmstr-yii2-cookie-consent)[richardfan1126/yii2-js-register

Yii2 widget to register JS into view

1357.2k7](/packages/richardfan1126-yii2-js-register)

PHPackages © 2026

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