If Only I Knew That!

Webby things that could save you time

MyCroft install

without comments

cd /mycroft
./build_host_setup.sh
sudo apt-get install curl espeak libssl-dev
./dev_setup.sh

Restart your OS and then

cd /mycroft
workon mycroft

Written by admin

May 24th, 2016 at 12:05 pm

Posted in mycroft

Send plain text email with Laravel 4

without comments

Laravel 4 takes the view and parses it and sends it as HTML

Mail::send('emails.welcome', $data, function($message)
{
    $message->to('foo@example.com', 'John Smith')->subject('Welcome!');
});

But what if you want to send HTML and plain text? Laravel makes that easy you need to use an array

Mail::send(array('emails.welcome.html', 'emails.welcome.text'), $data, function($message)
{
    $message->to('foo@example.com', 'John Smith')->subject('Welcome!');
});

But want if I just want to send just plain text. Once again Laravel has a simple solution. You need to use an associative array. This isn’t or wasn’t documented but all you have to do is look in the source.

Mail::send(array('text' => 'emails.welcome.text'), $data, function($message)
{
    $message->to('foo@example.com', 'John Smith')->subject('Welcome!');
});

The opposite key is ‘html’

Written by admin

September 17th, 2013 at 9:23 am

Posted in Laravel

Laravel 4 and Facebook PHP SDK

with 2 comments

After spending many hours trying to work out why I couldn’t get the ‘signed_request’ I found laravel 4 redirects to remove the trailing slash with the redirectIfTrailingSlash method. Facebook app URL’s have to have the trailing slash on the end or be a file i.e. ‘/’ or ‘index.php’

If you test your app and point it to the ‘/’ route the app works fine as the redirectIfTrailingSlash method doesn’t redirect. So how do you get a deeper route to work? Lets take a look at the route for ‘tab’, we have to use the post or any as Facebook posts the data to the route, you also need to include the trailing slash as you’ll get a 404 otherwise

The route needs to be

// I name space my controllers but you can see the important bit to define the route
Route::any('tab/', array('as' => 'tab.index', 'uses' => 'App\\Controllers\\TabController@anyIndex'));

Next we need to address the redirectIfTrailingSlash method, I do this by extending \Illuminate\Foundation\Application class and override the redirectIfTrailingSlash method to include a excludePaths in the condition, like so

There is probably a more dynamic way to do this but this provides a working solution

<?php namespace App\Extensions\Foundation;

use Symfony\Component\HttpFoundation\RedirectResponse as SymfonyRedirect;

class Application extends \Illuminate\Foundation\Application {

    /**
     * Redirect the request if it has a trailing slash.
     *
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|null
     */
    public function redirectIfTrailingSlash($excludePaths = array())
    {
        if ($this->runningInConsole()) return;

        // Here we will check if the request path ends in a single trailing slash and
        // redirect it using a 301 response code if it does which avoids duplicate
        // content in this application while still providing a solid experience.
        $path = $this['request']->getPathInfo();

        if ($path != '/' and ends_with($path, '/') and ! ends_with($path, '//') and ! in_array($path, $excludePaths))
        {
            with(new SymfonyRedirect($this['request']->fullUrl(), 301))->send();

            exit;
        }
    }
}

And the last thing to do is change the creation of Application call

// bootstrap/start.php
//$app = new Illuminate\Foundation\Application;
$app = new App\Extensions\Foundation\Application;
$app->redirectIfTrailingSlash(array('tab/'));

Note you’ll need to load the extended Application class 🙂

EDIT:
There are changes with Facebook and L4.1

A facebook page tab doesn’t require the trailing slash at the end of the URL anymore but the canvas page still does.

L4.1 redirects the trailing slash at .htaccess not in the foundation application class as it did previously

# Redirect Trailing Slashes…
RewriteRule ^(.*)/$ /$1 [L,R=301]

Just comment it out

Written by admin

August 21st, 2013 at 1:58 pm

Posted in Facebook,Laravel

Codeception with Laravel 4

with one comment

To use codeception with Laravel 4

Install via composer

composer require "codeception/codeception:@stable"

Update composer if you add the require manually

composer update

Bootstrap codeception with the app directory path

codecept bootstrap app

Set the PhpBrowser url config in app/tests/acceptance.suite.yml

class_name:WebGuy modules:
    enabled:
        - PhpBrowser
        - WebHelper
    config:
        PhpBrowser:
            url: 'http://localhost:8888'

Move the codeception.yml file to the root directory otherwise you have to use the -c option to set the path on each call

mv app/codeception.yml codeception.yml

Adjust the paths in codeception.yml to reference app/tests

paths:
    tests: app/tests
    log: app/tests/_log
    data: app/tests/_data
    helpers: app/tests/_helpers

Test away

Written by admin

July 30th, 2013 at 8:30 pm

Posted in Laravel,PHP

Add vendor/bin to your path for use with composer

without comments

In your .bashrc file on OSX add

export PATH=vendor/bin:$PATH

Then you can call codeception if composer installed using the short version of just the name

codecept

instead of

vendor/bin/codecept

Written by admin

June 4th, 2013 at 9:18 am

Posted in Codeception,OSX,PHP

Laravel 4 ORM query scope

without comments

It’s been a while and 2 frameworks later from Codeigniter, I’m using the Laravel 4 with composer. Life’s got a lot easier and quicker. Here is a quick snippet, that I’ll reference at some stage.

ORM query scope has been added to Laravel 4. It’s as simple as this

class User extends Eloquent {

     public function scopeApproved($query)
     {
          $query->where('approved', 1);
     }

    public function scopePopular($query)
    {
          $query->where('votes', '>', 100);
     }

}

and to use the query

$users = User::approved()->popular()->get();

If you want to pass additional parameters to the query

class User extends Eloquent {

     public function scopeApproved($query)
     {
          $query->where('approved', 1);
     }

    public function scopePopular($query, $minimum = 100)
    {
          $query->where('votes', '>', $minimum);
     }

}

and to use it

$users = User::approved()->popular(50)->get();

Original info is here

Written by admin

March 22nd, 2013 at 7:44 am

Posted in Laravel

Using MySQL NOW() in Codeigniter

without comments

There are two easy ways to do this

With NOW()

$data = array(
    'field_name' => 'some_data'
);
$this->db->set('date_field_name', 'NOW()', FALSE);
$this->db->insert('table_name', $data);

The FALSE in the set method prevents the NOW() being escaped

Without NOW()

$now = date('Y-m-d H:i:s');
$data = array(
    'field_name' => 'some_data',
    'date_field_name' => $now
);
$this->db->insert('table_name', $data);

Written by daveganley

May 5th, 2011 at 9:23 am

Posted in Codeigniter

New facebook page profile picture

without comments

After wasting time on several occasions looking for this, I thought I’d post it here so next time, I can find it quickly.

Profile picture reduced from 200 x 600 pixels to 180 x 540

Written by daveganley

March 29th, 2011 at 4:56 pm

Posted in Facebook

Quick tip: Codeigniter config for index.php when using .htaccess

without comments

Can across this the other day by dhorrigan on forrst.com

It checks for .htaccess file and if not found uses index.php

$config['index_page'] = is_file(FCPATH . '.htaccess') ? '' : 'index.php';

Written by daveganley

September 21st, 2010 at 5:08 pm

Posted in Codeigniter,PHP

Codeigniter 2.0 configurable profiler + some jQuery magic to show/hide

with 3 comments

The new CI 2.0 Profiler has configurable sections + 2 new additional sections (http_headers and config).

You’ll need to edit application/config/profiler.php

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| Profiler Sections
| -------------------------------------------------------------------------
| This file lets you determine whether or not various sections of Profiler
| data are displayed when the Profiler is enabled.
| Please see the user guide for info:
|
|	http://codeigniter.com/user_guide/general/profiling.html
|
*/

$config['benchmarks'] 		= TRUE;
$config['get'] 				= TRUE;
$config['memory_usage'] 	= TRUE;
$config['post'] 			= TRUE;
$config['uri_string']		= TRUE;
$config['controller_info']	= TRUE;
$config['queries'] 			= TRUE;
$config['http_headers'] 	= TRUE;
$config['config'] 			= TRUE;

/* End of file profiler.php */
/* Location: ./application/config/profiler.php */

I previously used a modified version of MY_Profiler from dragffy.com. I modified it to add a button to the top of the page that would show or hide the profiler when needed and could also hide the profiler button totally.

Click to continue reading “Codeigniter 2.0 configurable profiler + some jQuery magic to show/hide”

Written by daveganley

May 4th, 2010 at 1:22 pm

Posted in Codeigniter,PHP

Powered by CDN Rewrites

WordPress Loves AJAX