Last active
August 29, 2015 14:13
-
-
Save you-think-you-are-special/987c6536122cc5c8cc76 to your computer and use it in GitHub Desktop.
Yii2 Url Behavior
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Email: <[email protected]> | |
* 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]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Хорошая возможность.