Is it possible to add in Laravel asset() function?

I want the asset of my site to be loaded as instead of path.

without using any cloud CDN.

In short I want this <img src="{{asset('/js/app.js')}}" > =<img src="" >

to be in this form

<img src="{{asset('/js/app.js')}}" > =<img src="" >

Please have a look at this package as well this is doing the same but not supported for laravel 8

Any help will be appreciated.
Thanks in advance

Note: I am using Laravel 8


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can configure the asset URL host by setting the ASSET_URL variable in your .env file. This can be useful if you host your assets on an external service like Amazon S3 or another CDN:


$url = asset('js/app.js'); //


Method 2

I solve it myself by adding middleware and changing the response sent to the browser.

Inspired by Above mentioned package.


  namespace AppHttpMiddleware;

  use Closure;
  use IlluminateHttpRequest;

 class CdnMiddleware
 * Handle an incoming request.
 * @param  IlluminateHttpRequest  $request
 * @param  Closure  $next
 * @return mixed
public function handle(Request $request, Closure $next)
    $response = $next($request);
    $output = $response->getOriginalContent();
    $rules = array(

            // 'url'        => array('', ''),
            // 'remove' => 'assets/images/',
            'pattern'   => 'png|tif|tiff|gif|jpeg|jpg|jif|jfif|jp2|jpx|j2k|j2c|ico',
            'enabled'   => true
            'pattern'   => 'css',
            'enabled'   => true
            'pattern'   => 'js',
            'enabled'   => true
            'pattern'   => 'asf|avi|flv|m1v|m2v|m4v|mkv|mpeg|mpg|mpe|ogg|rm|wmv|mp4|webm',
            'enabled'   => true
        $url =  array(

    if (is_string($output) || method_exists($output, '__toString')) {
        $urls = preg_match_all("#bhttps?://[^s()<>]+(?:([wd]+)|[^[:punct:]s]|/)#", $output, $matches);

        foreach ($matches[0] as $uri)
            foreach ($rules as $group)
                if (@$group['enabled'] && preg_match('/.(' . @$group['pattern'] . ')(?:[?#].*)?$/i', $uri, $matchess))
                    $config_url = $url;

                    $config_remove = '';
                    $main_url = URL::to('/');
                    $asset_path = str_replace(str_finish(strval($main_url), '/'), '', $uri);

                    // check for external URLs. so far all the urls with current hostname
                    // has been removed. If there is a still URL which has http://---- that means
                    // its an external URL
                    $extMatch = [];
                    $extUrl = preg_match("#bhttps?://[^s()<>]+(?:([wd]+)|[^[:punct:]s]|/)#", $asset_path, $extMatch );


                    if (isset($group['url']))
                        $config_url = $group['url'];

                    if (isset($group['remove']))
                        $config_remove = $group['remove'];

                    if ($config_remove)
                        $asset_path = str_replace(str_finish($config_remove, '/'), '', $asset_path);

                    $cdn_url = is_array($config_url) ? $config_url[array_rand($config_url)] : $config_url;
                    $output = str_replace($uri, str_finish($cdn_url, '/') . $asset_path, $output);

    return $response;


All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x