Bạn Tìm Gì Hôm Nay ...?
Tất cả đều có chỉ trong 1 nốt nhạc !
Nếu cần hỗ trợ chi tiết gọi 1900 9477
Để tích hợp CDN cho Laravel đầu tiên cần phải có CDN
Nếu chưa đăng ký có thể tham khảo các gói CDN của PAvietnam tại đây
Để tạo custom helper đầu tiên cần tạo thư mục app/Helpers
Tạo file app/Helpers/CDN.php với nội dung dưới đây
<?php
if (!function_exists('cdn')) {
function cdn( $asset ){
// Verify if KeyCDN URLs are present in the config file
if( !Config::get('app.cdn') )
return asset( $asset );
// Get file name incl extension and CDN URLs
$cdns = Config::get('app.cdn');
$assetName = basename( $asset );
// Remove query string
$assetName = explode("?", $assetName);
$assetName = $assetName[0];
// Select the CDN URL based on the extension
foreach( $cdns as $cdn => $types ) {
if( preg_match('/^.*\.(' . $types . ')$/i', $assetName) )
return cdnPath($cdn, $asset);
}
// In case of no match use the last in the array
end($cdns);
return cdnPath( key( $cdns ) , $asset);
}
}
if (!function_exists('cdnPath')) {
function cdnPath($cdn, $asset) {
return "//" . rtrim($cdn, "/") . "/" . ltrim( $asset, "/");
}
}
Để Laravel tự động load file helper cần tạo thêm service provider. Để tạo một service provider sử dụng lệnh dưới đây
php artisan make:provider HelperServiceProvider
Sau đó mở file app/Providers/HelperServiceProvider.php xoá toàn bộ nội dung bên trong file và thay thế bằng nội dung sau:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class HelperServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
foreach (glob(app_path() . '/Helpers/*.php') as $file) {
require_once($file);
}
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}
}
Mở file config/app.php và thêm vào block providers
Để Laravel load toàn bộ nội dung tĩnh bao gồm hình ảnh, js, css từ link CDN cần thêm vào cuối file config/app.php nội dung sau:
/* |-------------------------------------------------------------------------- | CDN |-------------------------------------------------------------------------- */ 'cdn' => array( "Link CDN" => "jpg|jpeg|png|gif|svg|ico|css|js|eot|woff|ttf", ),
Thay đổi toàn bộ link js, css, hình ảnh thành link CDN. Cách gọi link js, css, hình ảnh lúc này sẽ như sau:
// Image
<img src="{{%20cdn(%20"/img/yourImg.png" ) }}" alt="Image" />
// CSS
<link rel="stylesheet" href="{{%20cdn('frontend/css/main.css')%20}}">
// js
<script src="{{%20cdn('frontend/js/main.js')%20}}"></script>
P.A Việt Nam cung cấp đa dạng các cấu hình Máy Chủ Ảo và Máy Chủ Riêng
Cloud Server
Cloud Server Pro
Máy Chủ Riêng
Tham khảo các ưu đãi: https://www.pavietnam.vn/vn/tin-khuyen-mai/