Your data. Anywhere you go.

New Relic for iOS or Android

Download on the App Store    Android App on Google play

New Relic Insights App for iOS

Download on the App Store

Learn more

Close icon

Relic Solution: Improving the transaction naming on Yii2


Since the actual release of the PHP Agent doesn’t offer support to the PHP framework Yii2 (reference), some transactions are being reported in unexpected ways or even not being reported at all.

An example of this situation is all the transactions being reported as index.php regardless of what controller/action created them.


The solution

The solution to this problem is well known by our community, which would be to use PHP Custom Instrumentation were using the functions provided by the PHP Agent, you can start to instrument your data manually.

Among the custom instrumentation functions, we have the newrelic_name_transaction Agent API method, that basically allows you to set custom names to the transactions reported by the PHP Agent.

This approach consists basically of calling the newrelic_name_transaction inside each action to give a more meaningful naming structure.

The road blocker on this is: it is not efficient to repeat this piece of code inside all single actions of the application.

The eureka :bulb: moment

In order to make this approach more efficient, we suggest taking advantage of the Yii2 before_action method that is present inside the default Yii class Controller. This is the base class for all classes containing controller logic and all the controllers are supposed to extend it.

As per the Yii2 documentation, this function is called right before the actions and including the newrelic_name_transaction call inside it would solve the problem to all actions of that controller.

class OurController extends Controller
    public function beforeAction($action)
        $nr_transactionName = Yii::$app->controller->id.'/'.Yii::$app->controller->action->id;
        if (extension_loaded('newrelic')) { // Ensure PHP agent is available
        return parent::beforeAction($action);

    public function actionIndex()
        return $this->render('index');

    public function actionExample()
        . . .

As shown in the example above, the function beforeAction was overwritten inside OurController and the name of the transaction is set by the combination of the name (ID) of the Controller that was called and the action called. In that way, all the actions of OurController are going to be named by the newrelic_name_transaction.

Going forward, if OurController is a custom base controller of the application all other controllers that would extend it also would have the naming by default.

One important rule to keep in mind is to avoid dynamic naming when using the newrelic_name_transaction API method in order to avoid problems related to the overflow of metrics reported to New Relic dashboard resulting in a Metric Grouping Issue.


This is a great solution, but if you do not want to override the base controllers, another option is to include the on beforeAction in your config files which will inject itself in any controller automatically.

By adding:

'on beforeAction' => function ($event) {
  $nr_transactionName = Yii::$app->controller->id.'/'.Yii::$app->controller->action->id;
  if (extension_loaded('newrelic')) { // Ensure PHP agent is available

in your web.php and console.php ( in the basic app ), and common/main.php ( in the advanced app ).
It will automatically inject the beforeaction as per the overriden controller mentioned above.


That’s another awesome solution! Nice work @hugor :smiley:


Wow… !

This config I didn’t know!

Thank you @hugor!