YiiLinkable 1

A simple extension for Yii allowing easy and consistent access to model URLs and appropriate anchor text.

phpnode/YiiLinkable

Written in PHP by 1 contributor

Contribute

Need Support?


we offer maintenance, support and development services for all our open source projects.

Contact Us

Linkable

A simple behavior for Yii allowing easy and consistent access to the URLs of controller actions that relate to a particular model. When linkable is attached, the behavior makes createUrl() and createLink() available as methods on the model. As their names suggest, createUrl() returns a URL that points to the default controller action for the model and createLink() returns a link with the appropriates URL and anchor text for that model.

For example, if you have a User model that is primarily associated with UserController, you can attach the ALinkable behavior to your User model, and use $user->createLink("view") to retrieve a link to the view action on the UserController with the appropriate GET parameters for that user. Linkable also takes care of generating the appropriate anchor text for links, so it's easy to simply call $model->createLink() on any model with the behavior attached and by default you should always get back a sensible link. This makes it possible to write components that consume any kind of model and can always provide a user friendly way to link back to the primary page for that model. For example you might have a commenting system that allows commenting on any kind of model, and you want to be able to link back to the parent model with the appropriate anchor text etc, ALinkable provides that in a consistent way.

Additionally, if you need to change your link strategy in future, perhaps tweaking anchor text or switching to friendlier URLs relying on a different model attribute, you can simply change the behavior configuration to have your settings applied site-wide.

Installation

Extract the files to a folder in your application, e.g. protected/extensions/linkable and make sure the contents of this directory are imported by specifying the following in your main application config:

    "import" => array(
        "ext.linkable.*",
    ),
    ...

Configuring ALinkable

To add the linkable behavior to a model, add the following to your model's behaviors() method:

    public function behaviors()
    {
        return array(
            "ALinkable" => array(
                "class" => "ALinkable",
                // set the main route to the controller for this model
                "controllerRoute" => "/moduleName/controllerName",
                // the default action to use when linking to models of this type
                "defaultAction" => "view",
                // the attributes to include in the URL for the model
                "attributes" => array("id"),
                // the template to use when creating the anchor text for links.
                // model attributes can be included in the anchor text by enclosing
                // the attribute name in brackets. By default attributes will be treated
                // as text and will be html encoded before being rendered. It is possible
                // to change this by specifying the format for an attribute preceded by a
                // colon, e.g. "{thumbnailUrl:image}" would display the attribute
                // "thumbnailUrl" formatted as an image, whereas "{someHtml:raw}" would
                // display the attribute "someHtml" without html enconding the value
                "template" => "{name}, {id:number}",
            ),
        );
    }

The model can now be automatically linked to using the syntax:

    $model->id = 123456;
    $model->name = "Firstname Lastname";

    echo $model->createUrl(); // http://example.com/moduleName/controllerName/view?id=123456
    echo $model->createUrl("update"); // http://example.com/moduleName/controllerName/update?id=123456
    echo $model->createUrl("view", array("greeting" => "hello world")); // http://example.com/moduleName/controllerName/view?id=123456&greeting=hello%20world

    echo $model->createLink(); // <a href="http://example.com/moduleName/controllerName/view?id=123456">Firstname Lastname, 123.456</a>
    echo $model->createLink("Some label"); // <a href="http://example.com/moduleName/controllerName/view?id=123456">Some label</a>
    echo $model->createLink("Some label", "update"); // <a href="http://example.com/moduleName/controllerName/update?id=123456">Some label</a>
    echo $model->createLink("Some label", array("update", "foo" => "bar")); // <a href="http://example.com/moduleName/controllerName/update?id=123456&foo=bar">Some label</a>
    echo $model->createLink("Some label", "view", array("class" => "test")); // <a class="test" href="http://example.com/moduleName/controllerName/view?id=123456">Some label</a>

Doesn't this break MVC separation?

Yeah, kinda. It depends how you view (ha!) your models. If you're prepared to think of models as resources, implicitly accessed via certain controllers (which is the case in probably 90% of apps), then I think this is a pragmatic solution, if not the "purest".





43 other PHP projects


phpwkhtmltopdfPHP 716

A slim PHP wrapper around wkhtmltopdf with an easy to use and clean OOP interface

php-pdftkPHP 213

A PDF conversion and form utility based on pdftk

yii2-localeurlsPHP 189

Automatic locale/language management for URLs

YiiRedisPHP 131

A set of wrappers for different data types in redis

yiipasswordPHP 81

Password strategies for Yii

yii2-dockerizedPHP 64

A template for docker based Yii 2 applications

YiiSolrPHP 50

A wrapper for the pecl solr library that provides common Yii constructs, such as models, data providers etc

php-shellcommandPHP 48

A simple object oriented interface to execute shell commands in PHP

Yii-Docs-GeneratorPHP 47

Generates HTML documentation for Yii applications

YiiGitPHP 36

A git wrapper for Yii, allows access to all git commands programatically

YiiElasticSearchPHP 32

Elastic Search client for Yii

YiiCurlPHP 30

A curl library for Yii

YiiStateMachinePHP 28

A state machine behavior for Yii

yii2-streamlogPHP 19

A Yii 2 log target for streams in URL format

oauth2yiiPHP 18

An OAuth2 client / server extension for the Yii framework

packagecompressorPHP 16

A Javascript/CSS compressor based on Yii's package system

restyiiPHP 16

A RESTful extension for Yii.

localeurlsPHP 12

Automatic locale/language management for URLs

php-excel-readerPHP 12

It reads the binary format of XLS files directly and can return values and formats from any cell. This project is the fork of http://code.google.com/p/php-excel-reader/ that, apparently, is no longer maintained.

pdfablePHP 10

A Yii extension to create PDFs with PHPWkHtmlToPdf/wkhtmltopdf

yii-api-vimPHP 8

Yii API manual plugin for VIM

yii2-excel-messagePHP 7

Translate messages via Excel files

xcrudcontrollerPHP 7

A base class to quickly build customized CRUD interfaces.

yii2-bs3activeformPHP 7

A Bootstrap 3 enhanced ActiveForm for Yii 2

Yii-Package-ManagerPHP 6

A package manager for Yii

php-tmpfilePHP 6

A convenience class for temporary files

handlebarsphpPHP 6

Transpiles handlebars templates into native PHP templates

yii2-apidoc-vimPHP 5

Yii2 apidoc as Vim helpfiles

php-orientdbPHP 5

A fast PHP driver for the OrientDB binary protocol.

YiiUsersPHP 4

User management for Yii

translatablePHP 4

Transparent attribute translation for ActiveRecords

Yii-Resource-ManagerPHP 3

Helpers to allow Yii to interact with resources (files) of different types

flushablePHP 3

Yii dependency that allows to flush records from the cache

bs3activeformPHP 2

A lightweight utility to render Bootstrap 3 forms in Yii

YiiEmailerPHP 2

Emailing functions for Yii

AccessRestrictablePHP 2

A Yii ActiveRecordBehavior that automatically applies conditions for access restriction to every query.

YiiYaaPHP 1

YAA is an additional abstraction layer for Yii that aggregates a number of child models into a clean single model that is easy to cache

hybridauthmanagerPHP 1

An AuthManager for Yii that stores the hierarchy in a flat PHP file and the assignments in DB

defaultpersisterPHP 1

Yii extension to save and restore model values in user session.

YiiAbstractArrayModelPHP

Work with PHP files in Active Record way

twittonioPHP

simple useless twitter client

yii2-base-appPHP

An alternative Yii2 application template for purists

xreturnablePHP

Yii extension to create URLs that allow to return to a page by storing its GET Parameters on a stack.