Namespacing Models in Laravel

Posted by & filed under Laravel, PHP.

I have been using Laravel quite a bit recently and it’s a great framework, which is really good fun to code with. I’ve just started writing a registration system in Laravel and came across a problem which I felt was worth sharing. I was creating the table and model to store the event details and I found that the Event class caused a conflict with one of Laravel’s core libraries. The easy work around is to append _model to the model name. But this detracts from laravel’s expressive syntax, for example:

Event::find(1);

Is much more elegant than:

Event_model::find(1);

This is where namespaces come in, namespaces are a great way to group code to prevent naming collisions. All I do is define a Models namespace at the top of the model.

Namespace Models;
use Eloquent; // If your model extends Eloquent you will need this line

And then in each of the controllers that I need to use a model just add this line at the top of the file

use Models\Event;

This means that I can carry on using Laravel’s nice expressive syntax without worrying about naming collisions.

Show Woocommerce stock status for unmanaged products

Posted by & filed under Woocommerce, WordPress.

The WooCommerce stock status (In Stock, Out of stock) will only show if WooCommerce is managing the stock for you. I was working on a project and the client wanted the ability to manually set if the product was in stock or not. The code snippet below will allow you to show the stock status for both managed and unmanaged products. Just copy and paste into your themes functions.php file.

// Show the status for unmanaged stock products
add_filter( 'woocommerce_get_availability', 'show_stock_status_for_unmanaged_products', 1, 2);
function show_stock_status_for_unmanaged_products( $availability, $_product )
{
	// Don't do anything if managing stock
	if($_product->managing_stock()) return $availability;

	if($_product->is_in_stock()) :
		$availability['availability'] = __('In Stock', 'woocommerce');
		$availability['class'] = '';
	else :
		$availability['availability'] = __('Out of Stock', 'woocommerce');
		$availability['class'] = 'out-of-stock';
	endif;

	return $availability;
}

WP-Admin broken after updating WordPress

Posted by & filed under MAMP, WordPress.

Have you just updated your local of WordPress and its now all broken on MAMP, I’ve had this problem a few times now so thought it would be worth sharing how to fix this. The problem is caused by the load-scripts.php file being compressed with zlib, I first did as suggested on this stackoverflow question. But this still didn’t fix the problem after some looking around I noticed that MAMP zlib compression was turned off, when I turned this on WordPress worked fine. I’m not sure why this would fix it as zlib will only send compressed data if the browser lets apache know through the headers. I have a feeling this could be a bug with Chrome, or Mamp sending compressed data when the browser is not expecting it.

To fix this problem you will need to open /Applications/MAMP/conf/php5.4.4/php.ini in your text editor of choice, then search for zlib and you should find the line below.

zlib.output_compression = Off

You just need to change this line to

zlib.output_compression = On

Then just restart apache with apachectl restart and hopefully everything should be working again for you.

NOTE: After taking these steps I installed a new copy of WordPress and had the problem again, after restarting apache again the problem went away.

Backing up MySQL Databases

Posted by & filed under Bash, MySQL.

As a web developer you learn that there is nothing more important than keeping regular backups of your databases. I’ve created a little bash script to help with Backing up MySQL databases for this I’ll use mysqldump. Mysqldump is a nifty little utility that lets you dump the contents of a database to a file, so I use this to create a little bash script that will backup the database at regular intervals, and I’ll even chuck in deleting backups older than 30 days so you don’t over run with files.

First lets state the obvious security warning here, this script will contain details of your database so make sure you set the correct permissions, I use 700 but this may change depending on your hosting provider. Also don’t place the backup folder in your public_html folder otherwise people will be able to see the contents of your database and again lets make sure the .sql have the correct permissions otherwise over users on your server will be able to access them. Read more on “Backing up MySQL Databases” »

Website relaunch

Posted by & filed under Random.

I’ve found that one of the best parts of putting my site live in May earlier this year, was being able to start again and use what I had learnt to improve my site. Today I have launched my new design and I couldn’t be happier with it.

I think as a web developer/designer it’s easy procrastinate to much over your own site. When we work on a site for a client we have a specification and deadline to meet so the process is much simpler as you know what you need to deliver and when. Read more on “Website relaunch” »

PHP Insert into array at position

Posted by & filed under PHP.

The other day I found myself needing to insert into an array at a given position, so I wrote this little function. For example if you have pulled a list of posts from a database and are going to loop through these to output the HTML, but want to insert an advert after the third post, you can simply insert into an array at the third position.

The function is pretty simple and takes 3 parameters, the existing array $array, the data to be inserted $var and the position $position. The function then splits the array in two at the given position and merges the first array with the data and then merges again with the 2nd array. The function converts the data to be inserted to an array so you can pass any data type.

function array_insert($array, $var, $position)
{
$before = array_slice($array, 0, $position);
$after = array_slice($array, $position);

$return = array_merge($before, (array) $var);
return array_merge($return, $after);
}

You can also see view the gist.

Using composer with MAMP’s version of PHP

Posted by & filed under MAMP, PHP.

After my laptop has been running really for a couple of weeks now, I have decided to order some new RAM and do a clean install of Lion. This has given me a great opportunity to configure my development environment in a cleaner and more organised way.

As a developer I do install a number of packages to play around with and then either decide its not for me or find there was a better way configuring them and then find myself in a bit of a muddle. Read more on “Using composer with MAMP’s version of PHP” »

Using MAMP and Pow on the same machine

Posted by & filed under MAMP.

I’ve been playing with Ruby on Rails recently and I’ve been using Pow as a rack server for the applications I’m working on. So I don’t have to keep running “rails server” when I want to test an app out. It’s a brilliant little tool from 37 Signals that they describe as a zero-config rack server and it is just that.

The only drawback to Pow is that it highjacks all localhost requests, this means that working with MAMP and Pow simultaneously can be tricky. The easiest and simplest solution would be to leave the MAMP port for apache to the default 8888 so you can access your htdocs folder from http://localhost:8888/ Read more on “Using MAMP and Pow on the same machine” »

Locking down WordPress

Posted by & filed under WordPress.

I just came across a free eBook called Locking Down WordPress from CodePoet featuring Rachel Baker, Brad Williams, and John Ford. It’s an interview style book asking the 3 pro’s how they handle security for their sites and is a must for anyone who uses WordPress for their clients as it will save headaches later on!