Content Delivery Network Package for Laravel 4
-
Add the package to your
composer.json
and runcomposer update
{ "require": { "vinelab/cdn": "*" } }
-
Add the service provider to
app/config/app.php
:'providers' => array( '...', 'Vinelab\Cdn\CdnServiceProvider', '...' ),
The service provider will register all the required classes for this package and will also alias
the Cdn
class to CdnFacadeProvider
so you can simply use the Cdn
facade anywhere in your app.
Publish the package config file:
php artisan config:publish vinelab/cdn
and check it out at app/config/packages/vinelab/cdn/cdn.php
In case you would like to have environment-based configuration app/config/packages/vinelab/cdn/[ENV]/cdn.php
Supported Providers:
- Amazon Web Services - S3 (Default)
'default' => 'aws-s3',
'aws' => [
's3' => [
'credentials' => [
'key' => '',
'secret' => '',
],
'buckets' => [
'my-backup-bucket' => '*',
]
]
],
'buckets' => [
'my-default-bucket' => '*',
'js-bucket' => ['public/js'],
'css-bucket' => ['public/css'],
'...'
]
Specify directories, extensions, files and patterns to be uploaded.
'include' => [
'directories' => ['public/dist'],
'extensions' => ['.js', '.css', '.yxz'],
'patterns' => ['**/*.coffee'],
],
Specify what to be ignored.
'exclude' => [
'directories' => ['public/uploads'],
'files' => [''],
'extensions' => ['.TODO', '.txt'],
'patterns' => ['src/*', '.idea/*'],
'hidden' => true, // ignore hidden files
],
Set the CDN URL:
'url' => 'https://s3.amazonaws.com',
Determines how many files to be uploaded concurrently.
Will clear the buffer when the
threshold
has been reached, be careful when setting this to a high value not to exceed what you have allowed in your PHP configuration.
'threshold' => 10,
To load your local assets (not the CDN assets) for testing or during development, set the bypass
option to true
:
'bypass' => true,
Upload your assets with
php artisan cdn:push
Now you can use the facade Cdn
to call the Cdn::asset()
function.
Note: the asset
works the same as the Laravel asset
it start looking for assets in the public/
directory:
{{Cdn::asset('assets/js/main.js')}}
// https://js-bucket.s3.amazonaws.com/public/assets/js/main.js
{{Cdn::asset('assets/css/main.css')}}
// https://css-bucket.s3.amazonaws.com/public/assets/css/main.css
If you want to use a file from outside the public/
directory anywhere in app/
you can use the Cdn::path()
function to do that:
{{Cdn::path('public/assets/js/main.js')}}
// https://js-bucket.s3.amazonaws.com/public/assets/js/main.js
{{Cdn::path('private/something/file.txt')}}
// https://css-bucket.s3.amazonaws.com/private/something/file.txt
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.