PHPackages                             sjaakp/yii2-linktitles - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. sjaakp/yii2-linktitles

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

sjaakp/yii2-linktitles
======================

Fetch link titles in the Yii 2.0 PHP framework

1.0.1(1y ago)04MITPHP

Since Feb 1Pushed 1y ago1 watchersCompare

[ Source](https://github.com/sjaakp/yii2-linktitles)[ Packagist](https://packagist.org/packages/sjaakp/yii2-linktitles)[ RSS](/packages/sjaakp-yii2-linktitles/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (3)Used By (0)

Yii2-LinkTitles
===============

[](#yii2-linktitles)

[![Latest Stable Version](https://camo.githubusercontent.com/9266b73d936919adeea29cfa0eb9e3a67b2fb2ac246c245305e50f7d913e90eb/68747470733a2f2f706f7365722e707567782e6f72672f736a61616b702f796969322d6c696e6b7469746c65732f762f737461626c65)](https://packagist.org/packages/sjaakp/yii2-linktitles)[![Total Downloads](https://camo.githubusercontent.com/6f6c1e0b07f31f78c2195c49c92ddc58fe7ec23ff5edd5b8b5ecd870b31cef13/68747470733a2f2f706f7365722e707567782e6f72672f736a61616b702f796969322d6c696e6b7469746c65732f646f776e6c6f616473)](https://packagist.org/packages/sjaakp/yii2-linktitles)[![License](https://camo.githubusercontent.com/a9d3effd5f34caf1c7027429fe9b5b0715bcb1a258a43a81dcc33f13ac69a90c/68747470733a2f2f706f7365722e707567782e6f72672f736a61616b702f796969322d6c696e6b7469746c65732f6c6963656e7365)](https://packagist.org/packages/sjaakp/yii2-linktitles)

**LinkTitles** is a [Yii 2.0](https://www.yiiframework.com/ "Yii") widget which adds titles to relative links. The titles will pop up in a toolbox window when hovering above the link. The titles are fetched from the site by means of JSON-calls. If the link already has a title, it will remain unchanged, and not be overwritten by **LinkTitles**.

A demonstration of the LinkTitles is [here](http://www.sjaakpriester.nl/software/linktitles).

Installation
------------

[](#installation)

Install **yii2-linktitles** in the usual way with [Composer](https://getcomposer.org/). Add the following to the require section of your `composer.json` file:

`"sjaakp/yii2-linktitles": "*"`

or run:

`composer require sjaakp/yii2-linktitles`

You can manually install **yii2-linktitles** by [downloading the source in ZIP-format](https://github.com/sjaakp/yii2-linktitles/archive/master.zip).

Prerequisites
-------------

[](#prerequisites)

For **LinkTitles** to work, some settings have to be done to the `urlManager` component in the site's main configuration file, usually called `web.php` or `main.php` in the `config`directory:

```
    'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            '/' => '/view',
            // other rules ...
        ],
        // ...

```

Many Yii 2 sites will have these settings anyway.

Using the LinkTitles widget
---------------------------

[](#using-the-linktitles-widget)

Using the **LinkTitles** widget in an Yii2 view file can be as simple as:

```

    ...

        Some HTML containing several relative links...

    ...

```

The HTML between `begin()` and `end()` can be as complicated as you like.

On loading the view, **LinkTitles** tries to retrieve the titles of all the relative links by means of a JSON call with a modified `href`. If the `href` of the link is `/message/42`, **LinkTitles** calls `/message/link-title/42`. The site is expected to return the title in JSON-format, like:

```
{ "title": "The title of Message 42" }

```

One way to accomplish this is by using `LinkTitleAction` to add an action `link-title` to the controller linked to, like so:

```

```

This assumes that the model (`Message`) has an attribute `title` which contains the link title.

If the link title is kept in another attribute, say `header`, set the `attribute` property of `LinkTitleAction` to its name, like so:

```
    public function actions()
    {
        return [
            'link-title' => [
                'class' => LinkTitleAction::class,
                'model' => Message::class,
                'attribute' => 'header'
            ]
        ];
    }

```

You may also write a custom function to handle the `link-title` function along these lines:

```
