Display Adsense ads easily in Laravel 6.x, 7.x, 8.x and 9.x
abhibunt/laravel-adsense is a Laravel package for display adsense ads easily in laravel 6.x, 7.x, 8.x and 9.x.
It currently has 0 GitHub stars and 12 downloads on Packagist (latest version 1.0).
Install it with composer require abhibunt/laravel-adsense.
Discover more Laravel packages by abhibunt
or browse all Laravel packages to compare alternatives.
Last updated
Package for easily including Google Adsense Ad units in Laravel 6.x, 7.x, 8.x and 9.x and Lumen. For use with Laravel 5.x use original Mastergalen/Adsense-Ads package.
In your project root run
The first step is using Composer to install the package and automatically update your composer.json file, you can do this by running:
composer require abhibunt/laravel-adsense
Run php artisan config:publish abhibunt/laravel-adsense.
Edit the generated config file in /config/adsense.php to add your ad units
return [
'client_id' => 'YOUR_CLIENT_ID', //Your Adsense client ID e.g. ca-pub-9508939161510421
'ads' => [
'responsive' => [
'ad_slot' => 1111111111,
'ad_format' => 'fluid',
'ad_full_width_responsive' => true,
'ad_style' => 'display:inline-block'
],
'rectangle' => [
'ad_slot' => 2222222222,
'ad_style' => 'display:inline-block;width:300px;height:250px',
'ad_full_width_responsive' => false,
'ad_format' => 'auto'
]
]
];
You need to update your application configuration in order to register the package so it can be loaded by Laravel, just update your config/app.php file adding the following code at the end of your 'providers' section:
config/app.php
<?php
return [
// ...
'providers' => [
MartinButt\Laravel\Adsense\Providers\AdsenseServiceProvider::class,
// ...
],
// ...
];
Go to bootstrap/app.php file and add this line:
<?php
// ...
$app = new Laravel\Lumen\Application(
dirname(__DIR__)
);
// ...
$app->register(MartinButt\Laravel\Adsense\Providers\AdsenseServiceProvider::class);
// ...
return $app;
Note: facades are not supported in Lumen.
You may get access to the Google Adsense Ads services using following facades:
MartinButt\Laravel\Adsense\Facades\AdsenseFacadeYou can setup a short-version aliases for these facades in your config/app.php file. For example:
<?php
return [
// ...
'aliases' => [
'Adsense' => MartinButt\Laravel\Adsense\Facades\AdsenseFacade::class,
// ...
],
// ...
];
In your terminal type
php artisan vendor:publish
or
php artisan vendor:publish --provider="MartinButt\Laravel\Adsense\Providers\AdsenseServiceProvider"
Lumen does not support this command, for it you should copy the file
src/resources/config/adsense.phptoconfig/adsense.phpof your project.
In adsense.php configuration file you can determine the properties of the default values and some behaviors.
Add {!! Adsense::javascript() !!} in your <head> tag.
To show ads, add {!! Adsense::ads('ads_unit') !!}, where ads_unit is one of your ads units in your config file (for example {!! Adsense::ads('responsive') !!}).
Use {!! Adsense::ads('ads_unit') !!} every time you want to show an ad.
Please see the CHANGELOG.md file for more information on what has changed recently.
The Google Adsense Ads is open-sourced software licensed under the MIT license.
Please see the LICENSE.md file for more information.