Skip to content

Instantly share code, notes, and snippets.

@you-think-you-are-special
Last active August 29, 2015 14:13
Show Gist options
  • Save you-think-you-are-special/987c6536122cc5c8cc76 to your computer and use it in GitHub Desktop.
Save you-think-you-are-special/987c6536122cc5c8cc76 to your computer and use it in GitHub Desktop.
Yii2 Url Behavior
<?php
/**
* Email: <alexander@codeordie.ru>
* Date: 19.01.15
* Time: 12:10
*
* use \behaviors\Url
* ...
*
* public function behaviors()
* {
* return [
* Url::className()
* ];
* }
*
*/
namespace \behaviors;
use yii\base\Behavior;
use \yii\helpers\Url as U;
/**
* Class Url
* @package common\behaviors
*/
class Url extends Behavior
{
public $id = 'id';
public $route = null;
/**
* @return string
*/
public function getUrl()
{
$id = $this->id;
if ($this->route == null) {
$shortClassName = (new \ReflectionClass($this->owner))->getShortName();
$controller = strtolower($shortClassName);
$this->route = $controller . '/view';
}
if ($this->owner->hasAttribute($id))
return U::to([$this->route, $id => $this->owner->$id]);
return U::to([$this->route]);
}
}
@frops
Copy link

frops commented Jan 19, 2015

Хорошая возможность.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment