PHP, Web Development, Yii Framework

Stop Newrelic from recording slow AJAX/general calls in Yii2

I recently had to stop Newrelic from recording my long polling tactics so I came up with:

<?php

namespace common\components;

use Yii;
use yii\base\Component;
use yii\base\BootstrapInterface;
use yii\web\Application;
use yii\web\Controller;

class Newrelic extends Component implements BootstrapInterface
{
	public $ignoreRoutes = [];
	
	public function bootstrap($app)
	{
		// delay attaching event handler to the view component after it is fully configured
		$app->on(Controller::EVENT_BEFORE_ACTION, function () use ($app) {
			$this->beforeAction($app);
		});
	}
	
	public function beforeAction($app)
	{
		if(extension_loaded('newrelic')){
			if(in_array(Yii::$app->getUrlManager()->parseRequest($app->request)[0], $this->ignoreRoutes)){
				newrelic_ignore_transaction();
			}
		}
	}
}
PHP, Yii Framework

Getting Yii2 debug to show on user role instead of IP

I recently had to get Yii2 debug to show based on whether I was an admin user or not due to not connecting to the site using static IPs.

Having the debug in production run for the admin user is really useful for fixing those problems which are not easily, or possibly, replicated down to development.

So I overrode the Module class for debug but found some problems with view paths. I ended up with:


<?php

namespace common\components;

use Yii;
use yii\debug\Module as DebugModule;
use common\models\User;

class Module extends DebugModule
{
	private $_basePath;
	
	protected function checkAccess()
	{
		$user = Yii::$app->getUser();

		if(
			$user->identity && 
			$user->can('admin')
		){
			return true;
		}
		return parent::checkAccess();
	}
	
	/**
	 * Returns the root directory of the module.
	 * It defaults to the directory containing the module class file.
	 * @return string the root directory of the module.
	 */
	public function getBasePath()
	{
		if ($this->_basePath === null) {
			$class = new \ReflectionClass(new yii\debug\Module('debug'));
			$this->_basePath = dirname($class->getFileName());
		}
	
		return $this->_basePath;
	}
}

PHP, Ramblings, Web Development

Why I refuse to earn money from Facebook games

I was recently approached, through various channels, to co-found a Facebook game.

Essentially the primise of it is to make something like Tetrus or Diamonds and add micro-transactions for bonuses and what not to make the users life on the game easier.

I will get right to the point and state that I turned down the offer.

Sure it is a quick buck, use about 5 hours making the game, £300 of Facebook advertising/marketing and “bingo”, you have something that could earn you double your normal wage in the space of a year. An easy and sustainable income if I may be honest. Once every few years you release a new game and solely you can hold down a wage. No need to work for Zenga or one of those other big companies, just you can earn the money.

So with the last paragraph in mind it seems like a no brainer to make a Facebook game and earn some easy money, and many do, just look at how many games there are; with an added audience of 1 billion users how can you go wrong? There will always be a suckers addiction and/or misery to feed off.

Wait, what? Addiction and/or misery? Did I really learn to program to do this? And this follows onto my reasoning for refusing to make this game.

Sure I could make this game, sure I could earn a lot of money, but can I really?

Like many others, I learnt to program in my spare time because I wanted to, not to make money. I was in the final year of my university Honors degree in Computing and I had to come up with a final project idea so I chose to make a YouTube replica from PHP. I had never touched PHP before that time nor any web development frankly, aside form a small Java project in my first year to make a simple HTML website running on Tomcat.

To cut to the point: despite doing all that work and still being one of the few in the class who actually completed a project I got a shitty grade; 41.4% in fact which almost failed me (39% would have been a fail) and caused me to lose the 2:1 mark I was heading for.

But that didn’t stop me and I went onto a job which involved that exact work and I can feel pride in my work.

How, after everything I went through to get this expertise and knowledge, could I feel pride in something that feeds on misery? I think I probably speak for quite a few programmers when I say that this isn’t what we learnt to program for.

That’s why I feel sorry for people who work for Zenga, they have to work knowing that they have to take pride in something that brings addiction and misery to many millions of Facebook users, stealing their money in the process; their programs are effectively a plague upon the internet. Maybe that is why I keep hearing such low morale at the company in question, aside from management issues.

Every worth while programmer knows that they need to feel pride for what they have accomplished, sure there will be those who learn to program to “get ahead” in the market or to earn money. But the real programmers, those who make the internet what it is today love to program and they need to feel pride in their work and what they do and know that it is benefiting others, not sucking their money using their misfortunes as an excuse.

I could not feel pride in making a Facebook game that makes a quick buck, though I must admit myself that the thought of doing such a thing is tempting, but for the minute I will resist. I will not convert over to the dark side and become effectively Lord Vader. I still have some moralities left in me.

As such I regretfully but honestly decline to make a Facebook game.

PHP, Web Development, Yii Framework

Definition of Yii2 Aliases

Following on from this good article by @KartikV: Yii 2.0: Yii 2 list of path aliases available with default basic and advanced app – I recently needed to use this list to understand what goes into each alias so that I could dupe it for compression in the console. Unfortunately I found no reference for understanding this and the descriptions in that article are not very useful so here is my attempt.

There are a few assumptions made here:

– I have a url of “http://localhost.com/yii” as my base URL
– My app root is at “/srv/www/webapp” in my OS (Linux)

So here is my list:

– @app = /srv/www/webapp/frontend
– @vendor = /srv/www/webapp/frontend/config/../../vendor
– @runtime = /srv/www/webapp/frontend/runtime
– @web = /yii (from “http://localhost.com/yii“)
– @webroot = /srv/www/webapp/frontend/web
– @common = /srv/www/webapp/common
– @frontend = /srv/www/webapp/frontend
– @backend = /srv/www/webapp/backend
– @console = /srv/www/webapp/console

PHP, Yii Framework

Disable Yii2 CSRF on specific actions

I needed to disable the Yii2 CSRF on specific actions recently, mainly due to the action being called from an external source.

What I did was extend the Request object like so:

<?php

namespace common\components;

use Yii;

class Request extends \yii\web\Request
{
	public $noCsrfRoutes = [];
	
	public function validateCsrfToken()
	{
		if(
			$this->enableCsrfValidation && 
			in_array(Yii::$app->getUrlManager()->parseRequest($this)[0], $this->noCsrfRoutes)
		){
			return true;
		}
		return parent::validateCsrfToken();
	}
}

and then added the request component to my config like so:

		'request' => [
			'class' => 'common\components\Request',
			'noCsrfRoutes' => [
				'order/calculate-ns-shipping'
			]
		],

And that works.