Skip to content

Instantly share code, notes, and snippets.

@DanielHudson
Last active August 29, 2015 14:16
Show Gist options
  • Save DanielHudson/a94e40e2e84001366fdd to your computer and use it in GitHub Desktop.
Save DanielHudson/a94e40e2e84001366fdd to your computer and use it in GitHub Desktop.
cdnify for laravel/elixir
<?php
if ( ! function_exists('cdnify'))
{
/**
* Adds a CDN to a path when a specified environment is active.
*
* @param string $path
* @param array $environment
* @param boole $elixir
*
* @return string|false
*/
function cdnify($path, $environment = ['production'], $elixir = true)
{
$path = $path ?: false;
if($path === false)
{
return false;
}
if($elixir === true)
{
$path = elixir($path);
}
if(in_array(env('APP_ENV'), $environment))
{
$path = config('cdn.cloudfront') . $path;
}
return $path;
}
}
@DanielHudson
Copy link
Author

  1. Put the above contents in app/Http/helpers.php
  2. Add the following to the composer.json autoload section.
    "files": ["app/Http/helpers.php"],
  3. Create config/cdn.php with:
<?php
return [
    'cloudfront' => 'https://d6zmzv98u0ndl.cloudfront.net'
];
  1. Usage example: <link href="{{ cdnify('css/app.css') }}" rel="stylesheet">

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment