All Projects → Locustv2 → yii2-linkable-behavior

Locustv2 / yii2-linkable-behavior

Licence: BSD-3-Clause license
Yii2 behavior to help creating urls easier

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to yii2-linkable-behavior

behavior-trait
Allows handling events via inline declared methods, which can be added by traits
Stars: ✭ 18 (+50%)
Mutual labels:  behavior, yii2, yii2-extension
yii2-behaviors
Collection of useful behaviors for Yii Framework 2.0
Stars: ✭ 25 (+108.33%)
Mutual labels:  yii2, yii2-behaviors, yii2-extension
yii2-translatable
Translatable behavior aggregates logic of linking translations to the primary model
Stars: ✭ 15 (+25%)
Mutual labels:  yii2, yii2-behaviors, yii2-extension
yii2-lets-talk
With this extension you can open chat with someone in popular messengers using the link on your website.
Stars: ✭ 15 (+25%)
Mutual labels:  yii2, yii2-extension
Yii2 Taggable Behavior
This extension allows you to get functional for tagging.
Stars: ✭ 83 (+591.67%)
Mutual labels:  behavior, yii2
Yii2 Save Relations Behavior
Validate and save automatically related Active Record models.
Stars: ✭ 125 (+941.67%)
Mutual labels:  behavior, yii2
Yii2 Enhanced Gii
Enhanced Yii2 Gii (generator) that generates related Models & CRUD
Stars: ✭ 183 (+1425%)
Mutual labels:  yii2, yii2-extension
yii2-mailqueue
Yii2 mail queue component for yii2-swiftmailer.
Stars: ✭ 15 (+25%)
Mutual labels:  yii2, yii2-extension
content
Content management system for Yii2
Stars: ✭ 54 (+350%)
Mutual labels:  yii2, yii2-extension
yii2-stat
Yii2 Multi Web Statistic Module (yametrika, google-analytic, own db-counter)
Stars: ✭ 18 (+50%)
Mutual labels:  yii2, yii2-extension
ar-search
Provides unified search model for Yii ActiveRecord
Stars: ✭ 31 (+158.33%)
Mutual labels:  yii2, yii2-extension
yii2-cashier
Yii2 Cashier provides an interface to Stripe's subscription billing services.
Stars: ✭ 43 (+258.33%)
Mutual labels:  yii2, yii2-extension
yii2-db
Database extensions for Yii 2.0 Framework 📦
Stars: ✭ 19 (+58.33%)
Mutual labels:  behavior, yii2
Yii2 Translate Manager
Translation Manager
Stars: ✭ 221 (+1741.67%)
Mutual labels:  yii2, yii2-extension
install
basic script for project installation
Stars: ✭ 17 (+41.67%)
Mutual labels:  yii2, yii2-extension
Ar Softdelete
Soft delete behavior for ActiveRecord
Stars: ✭ 188 (+1466.67%)
Mutual labels:  yii2, yii2-extension
yii2-bankcard-info
银行卡卡号分析(Yii2扩展)
Stars: ✭ 15 (+25%)
Mutual labels:  yii2, yii2-extension
Balance
Balance accounting (bookkeeping) system based on debit and credit principle
Stars: ✭ 162 (+1250%)
Mutual labels:  yii2, yii2-extension
Crontab
Yii2 extension for crontab support
Stars: ✭ 170 (+1316.67%)
Mutual labels:  yii2, yii2-extension
yii2-newsletter
Module for saving user contacts from newsletter form to database
Stars: ✭ 17 (+41.67%)
Mutual labels:  yii2, yii2-extension

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 Total Downloads Latest Unstable Version License

Installation

The preferred way to install the library is through composer.

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

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)

getUrlRoute($action = 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)

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 = [])

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

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

// <a href="https://github.com/user/view?id=123">Locustv2</a>
echo $user->hotLink;

// <a href="https://github.com/user/update?id=123">Locustv2</a>
echo $user->getHotlink('update');

// <a class="text-bold" href="https://github.com/user/profile?id=123&ref=facebook">Locustv2</a>
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. <span/> will be used instead of <a/>

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

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

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

// <a href="http://www.yoursite.com/user/photo/view?id=123&photoid=456&slug=my-first-photo">Locustv2</a>
echo $user->getHotlinkTo($photo);

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

// <a class="font-bold" href="http://www.yoursite.com/user/photo/update?id=123&photoid=456&slug=my-first-photo&ref=homepage">Locustv2</a>
echo Url::to($user->getHotlinkTo($photo, 'update', ['ref' => homepage], ['class' => 'font-bold']));

To do

  • Add unit tests

Contributing

Feel free to send pull requests.

License

For license information check the LICENSE-file.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].