carono / yii2-url-behavior
Yii2 behavior and URL rule component for dynamic URL generation based on model, action, user roles, and application context.
Package info
github.com/carono/carono-yii2-url-behavior
Type:yii2-extension
pkg:composer/carono/yii2-url-behavior
Requires
- php: ^7.4 || 8.*
- yiisoft/yii2: ^2.0
This package is auto-updated.
Last update: 2026-03-16 10:52:58 UTC
README
A flexible behavior for Yii2 that provides dynamic URL generation based on user roles, application context, and custom rules.
Features
- Role-based URL generation
- Multi-application support
- Flexible rule configuration
- Caching support for performance optimization
- Customizable URL rule parameters
Installation
Add the package to your composer.json:
{
"require": {
"carono/yii2-url-behavior": "*"
}
}
Usage
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
rules: Array of URL rules or a string method name that returns rulesdefaultUrl: Default URL when no rules matchfunctionAlias: Method name to expose (default:getUrl)authManager: Auth manager component ID (default:authManager)ruleClass: Custom rule class implementation
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
$post = Post::findOne(1);
// Get URL as array
$urlArray = $post->getUrl('view');
// Get absolute URL string
$urlString = $post->getUrl('view', true);
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
The UrlRule class provides these properties:
action: Target action nameurl: URL pattern or callablerole: Role(s) requiredapplication: Application restrictionparams: Parameter configurationauthManager: Auth manager componentcache: Cache component for role cachingmodel: Related model instance
Caching
The behavior supports role caching to improve performance:
'rules' => [
['view', 'url' => ['post/view'], 'role' => 'user', 'duration' => 3600]
]
Advanced Usage
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
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
MIT