If Only I Knew That!

Webby things that could save you time

Archive for the ‘Laravel’ Category

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

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

Powered by CDN Rewrites