Using CDN to serve images and maybe .css and .js files

Posted In General
  • stantyan 2 years ago
    Hi,
    
    Is it easy to implement CDN to serve images? Can you please help how can I do this?

    Hi,

    Is it easy to implement CDN to serve images? Can you please help how can I do this?

  • devdojo 2 years ago
    You should be able to do this fairly easy. Laravel supports multiple file systems out of the box.
    
    Take a look at this: [https://laravel.com/docs/5.4/filesystem](https://laravel.com/docs/5.4/filesystem),
    
    If you scroll down on how to use storage with S3, you will need to include this package in your laravel app:
    
    ```
    league/flysystem-aws-s3-v3 ~1.0
    ```
    
    I'll make sure to include it in the next release of the app as well.
    
    Then inside of the `config/filesystems.php` you will need to change the storage you are using from `local` to `s3`.
    
    I can add this to a future version so that way you can include this in your settings of your site and enable s3 instead of local storage.
    
    Is s3 the CDN you are looking to use? Let me know :)
    
    Thanks.

    You should be able to do this fairly easy. Laravel supports multiple file systems out of the box.

    Take a look at this: https://laravel.com/docs/5.4/filesystem,

    If you scroll down on how to use storage with S3, you will need to include this package in your laravel app:

    league/flysystem-aws-s3-v3 ~1.0
    

    I'll make sure to include it in the next release of the app as well.

    Then inside of the config/filesystems.php you will need to change the storage you are using from local to s3.

    I can add this to a future version so that way you can include this in your settings of your site and enable s3 instead of local storage.

    Is s3 the CDN you are looking to use? Let me know :)

    Thanks.

  • stantyan 2 years ago
    I want to use MaxCDN with Pull Zone method. Should I follow the same path as if I would use amazon s3? do I need to install any additional packages?

    I want to use MaxCDN with Pull Zone method. Should I follow the same path as if I would use amazon s3? do I need to install any additional packages?

  • devdojo 2 years ago
    Hi Stantyan,
    
    Right now it looks like Laravel supports Amazon s3 and Rackspace files. I'm sure we could get MaxCDN configured, but it may take a little custom work. If you are a subscriber on the DevDojo I can help out with that customization. Other then that s3 and Rackspace can be easily added and I will include the `league/flysystem-aws-s3` and the `league/flysystem-rackspace ~1.0` in the next release.
    
    Thanks. Talk to you soon.

    Hi Stantyan,

    Right now it looks like Laravel supports Amazon s3 and Rackspace files. I'm sure we could get MaxCDN configured, but it may take a little custom work. If you are a subscriber on the DevDojo I can help out with that customization. Other then that s3 and Rackspace can be easily added and I will include the league/flysystem-aws-s3 and the league/flysystem-rackspace ~1.0 in the next release.

    Thanks. Talk to you soon.

  • mark 2 years ago
    It would be possible to make a storage provider for Laravel that supports this.
    
    A package that might help doing this: https://github.com/davidsneal/laravel-maxcdn

    It would be possible to make a storage provider for Laravel that supports this.

    A package that might help doing this: https://github.com/davidsneal/laravel-maxcdn

  • stantyan 2 years ago
    Thank you so much Mark!! I will try this.

    Thank you so much Mark!! I will try this.

  • oliver-russell 2 years ago
    <p>Implementing CDN in your Laravel app is fairly easy.</p>
    
    <p>To implement Laravel CDN, create a global function which will be used throughout the application. For this, add a helpers.php file in the app folder. Source: https://www.cloudways.com/blog/integrate-cdn-in-laravel/</p>
    
    <p>function cdn( $asset ){</p>
    
    <p>   //Check if we added cdn's to the config file</p>
    
    <p>   if( !Config::get('app.cdn') )</p>
    
    <p>       return asset( $asset );</p>
    
    <p>   //Get file name &amp; cdn's</p>
    
    <p>   $cdns = Config::get('app.cdn');</p>
    
    <p>   $assetName = basename( $asset );</p>
    
    <p>   //remove any query string for matching</p>
    
    <p>   $assetName = explode("?", $assetName);</p>
    
    <p>   $assetName = $assetName[0];</p>
    
    <p>   //Find the correct cdn to use</p>
    
    <p>   foreach( $cdns as $cdn =&gt; $types ) {</p>
    
    <p>       if( preg_match('/^.*\.(' . $types . ')$/i', $assetName) )</p>
    
    <p>           return cdnPath($cdn, $asset);</p>
    
    <p>   }</p>
    
    <p>   //If we couldnt match a cdn, use the last in the list.</p>
    
    <p>   end($cdns);</p>
    
    <p>   return cdnPath( key( $cdns ) , $asset);</p>
    
    <p>}</p>
    
    <p>function cdnPath($cdn, $asset) {</p>
    
    <p>   return  "//" . rtrim($cdn, "/") . "/" . ltrim( $asset, "/");</p>
    
    <p>}
    </p>
  • cbmagent-agent 1 year ago
    Any update on this? MaxCDN would be great!

    Any update on this? MaxCDN would be great!

Please login or signup to leave a response.

Login

or Sign Up

Sign Up

or Login
×
Want to create your own SAAS?

I can teach you to create your own Software as a Service. Click here to learn more!

Visit SAAS Adventure!