Setting the Base URL for Assets in your Laravel Applications is very important to make sure all assets like stylesheets, images, and all javascript are properly loading and running without any issues.
Properly set or configured base URLs make sure that our application is accessible in differenttypes of environments like staging, and production.
1. asset() Helper Function
asset() helper function is very useful when it comes to loading assets of the application.
by default, this helps use the APP_URL value that is defined in the .env file.
2. APP_URL in the .env File
variable app_url in the configuration file decides the base URL of your application.
APP_URL=http://shoplocal.test
For production update it to the live URL
APP_URL=http://myabcshop.com
3. Steps to Set Base URL for Assets in Laravel Applications:
1. Using Content Delivery Network (CDN) for Hosting Assetsa) If you are planning to use CDN URLs for your Laravel application then. Open the environment configuration file .env in the root of your Laravel application. It looks something like the below screenshot.
b) Also add the CDN asset URL in AppServiceProvider
to register this custom asset URL:
public function boot()
{
if (env('ASSET_URL'))
URL::forceRootUrl(env('ASSET_URL'));
}
}
Additionally, if we add or update the public value in asset_url like
Add ASSET_URL=public
it will set the asset folder path to the public folder
c) Clear the configuration cache
No comments:
Post a Comment