In each controller in my Laravel application, I use all of this namespace:
use App\User;
use App\Http\Controllers\Controller;
use Google\Auth\OAuth2;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\App;
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\v201609\cm\CampaignService;
use Google\AdsApi\AdWords\v201609\cm\OrderBy;
use Google\AdsApi\AdWords\v201609\cm\Paging;
use Google\AdsApi\AdWords\v201609\cm\Selector;
use Google\AdsApi\AdWords\v201609\cm\SortOrder;
use Google\AdsApi\Common\OAuth2TokenBuilder;
I need a way to avoid repeating all this code in my controller. I have tried to put it in a base controller and then extend all other controller from this but it did't work.
I would be grateful for any suggestions.
If you are using PHP 7, you can group these.
Example
use Google\AdsApi\AdWords\v201609\cm\CampaignService;
use Google\AdsApi\AdWords\v201609\cm\OrderBy;
use Google\AdsApi\AdWords\v201609\cm\Paging;
use Google\AdsApi\AdWords\v201609\cm\Selector;
use Google\AdsApi\AdWords\v201609\cm\SortOrder;
Becomes
use Google\AdsApi\AdWords\v201609\cm\{CampaignService, OrderBy, Paging, Selector, SortOrder};
You can also do something like the following which should work on older versions of PHP.
use Google\AdsApi\AdWords;
$adWordsServices = new AdWords\AdWordsServices;
$campaignService = new AdWords\v201609\cm\CampaignService;
This cuts down on what you need to import but requires you to fill in the remainder of the namespace you did not import when instantiating.
http://php.net/manual/en/language.namespaces.importing.php#language.namespaces.importing.group
Looks like you have a lot of logic in your controllers. You could create more specific classes that does certain logic. Ex create a class that handles all GoogleApi interactions then in your controller import that class.
app/GoogleApi/AdWords.php
<?php
namespace App\GoogleApi;
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\v201609\cm\CampaignService;
use Google\AdsApi\AdWords\v201609\cm\OrderBy;
use Google\AdsApi\AdWords\v201609\cm\Paging;
use Google\AdsApi\AdWords\v201609\cm\Selector;
use Google\AdsApi\AdWords\v201609\cm\SortOrder;
use Google\AdsApi\Common\OAuth2TokenBuilder;
class AdWords
{
public function auth()
{
// auth logic
}
// other methods
}
Then in your controllers you'll only have
use App\GoogleApi\AdWords.
If you are using php 7 already have a good answer, if not you can do something like this:
Instead of :
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
...
put only
use Google\AdsApi\AdWords;
then in your code to use that namespace classes:
$service = new AdWords\AdWordsServices;
Hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With