Laravel use vendor directory php class - php

Recently I learn Laravel and try to implement BingAds SDK to grab some reports to my database but failed.
I have a controller named BingAdController.php in app/http/Controllers/BingAdController.php
BingAds SDK is installed via composer, they are in vendor/microsoft/bingads/samples/V12/ReportRequests.php
BingAdController.php
namespace App\Http\Controllers;
use SoapVar;
use SoapFault;
use Exception;
use SoapClient;
use Illuminate\Http\Request;
use App\BingAd;
use App\Http\Controllers\Controller;
use Microsoft\BingAds\V12\Reporting\ReportRequestStatusType;
use Microsoft\BingAds\Auth\ServiceClient;
use Microsoft\BingAds\Auth\ServiceClientType;
use Microsoft\BingAds\Samples\V12\AuthHelper;
use Microsoft\BingAds\Samples\V12\ReportRequestLibrary;
include("/Applications/MAMP/htdocs/laravel/vendor/microsoft/bingads/samples/V12/ReportRequests.php");
class BingAdController extends Controller {
public function bingadsReporting(){
ReportRequests.php
namespace Microsoft\BingAds\Samples\V12;
// require_once __DIR__ . "/vendor/autoload.php";
require_once "/Applications/MAMP/htdocs/laravel/vendor/autoload.php";
include __DIR__ . "/AuthHelper.php";
include __DIR__ . "/ReportRequestLibrary.php";
use SoapVar;
use SoapFault;
use Exception;
use Microsoft\BingAds\V12\Reporting\ReportRequestStatusType;
use Microsoft\BingAds\Auth\ServiceClient;
use Microsoft\BingAds\Auth\ServiceClientType;
use Microsoft\BingAds\Samples\V12\AuthHelper;
use Microsoft\BingAds\Samples\V12\ReportRequestLibrary;
$GLOBALS['AuthorizationData'] = null;
$GLOBALS['Proxy'] = null;
$GLOBALS['CampaignManagementProxy'] = null;
class ReportRequests {
public $DownloadPath, $length, $folder;
Laravel keep saying the class not found...
I have been stuck in this problem for 2 days... please help

You shouldn't be manually calling include or require_once in your code - Laravel uses Composer's autoloader out of the box, so you should just be able to refer to the classes you need and it will do the rest.
First, ensure that you have installed Bing's SDK through Composer - i.e. it's added in your composer.json and it was installed through the command line tool. If you just downloaded it yourself and dropped it in the vendor directory it's not going to work.
Then you should be able to call new Microsoft\BindAds\Auth\ServiceClient or whichever class you want - Composer will know where and how to find this class for you.
If you have installed it through Composer and are still having issues you'll need to provide the exact code you're having issues with as well as the full error and stacktrace you're seeing so that we can assist debugging it.

Finally I found the solution.
Add "vendor/microsoft/bingads" into vendor/composer.json -> classmap
Don't know why BingAds didn't auto update composer.json even via composer install.

Related

LARAVEL: main(): Failed opening required 'vendor\autoload.php'

I followed this documentation and I keep getting that main(): Failed opening required 'vendor\autoload.php' error and I ran composer install but still get the same error. I'm using Laravel and I'm calling this from a Controller..
namespace App\Http\Controllers;
require 'vendor/autoload.php';
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Google\Cloud\Speech\SpeechClient;
use Google\Cloud\Speech\StorageClient;
use App\Model\FilesModel;
use Illuminate\Support\Facades\DB;
class FilesController extends Controller
{
private $project_id;
private $speech;
private $options;
private $storage;
public function __construct()
{
$storage = new StorageClient([
'keyFile' => json_decode(file_get_contents(public_path() . '/key.json'), true)
]);
....
How do I bypass this issue?
first of all no need to do that! because it's included in all pages...
if you insist doing this I think the problem is the address of autoload file which have to be:
require '../vendor/autoload.php';
I solved deleting the "require '../vendor/autoload.php';" sentence from Controller and works in bouth environments (local and server). I am working with an Openpay integration.
That was hard for me, because i was trying to solve editing the routes or updating composer and stuff like that.

include the autoloader php

I am trying to install an SDK for OpenPay ( Mexican PayPal ) in laravel 5.2 and after doing the composer install it asks me to do this:
Finally, be sure to include the autoloader:
require_once '/path/to/your-project/vendor/autoload.php';
I dont know where to put that, or what to do of it, I just dont understand. If I continue without doing that everytime I try to create an instance like
$openpay = Openpay::getInstance();
I get an error saying
Class 'App\Http\Controllers\Openpay' not found
How can I fix this? Thank you!
When you did composer require openpay/sdk, composer already installed it. It's already part of your autoload. Now you can use it like this:
$openpay = \Openpay::getInstance();
Or add it to your controller first
...
use Openpay;
public function MyController extends Controller
{
$openpay = Openpay::getInstance();
}

Class 'App\Http\Controllers\Saml2' not found

I'm on Laravel 5, I'm trying to integrate SAML 2.0 with it. I've found this package = https://github.com/aacotroneo/laravel-saml2
I tried follow their steps, but at the end when I use
<?php
namespace App\Http\Controllers;
class SAMLController extends Controller {
public function adminSignIn(){
return Saml2::login(URL::full());
}
}
I've already added
provider
'Aacotroneo\Saml2\Saml2ServiceProvider',
aliases
'Saml2' => 'Aacotroneo\Saml2\Facades\Saml2Auth',
Why do I still get this error?
Class 'App\Http\Controllers\Saml2' not found
Note : I've even retry after sudo composer dumpauto, same result.
You need to use full namespace for the facade:
\Saml2::login(URL::full());
Or add this to the top of the class:
use Saml2;
you need to explicitly write "use" on top
use Saml2;
This might work.

Simple PHP namespace issue

Trying to implement a 3rd-party-lib with PHP 5.3.9-ZS5.6.0 and the lib uses namespaces. The code where I want to use the lib doesn't. So i have:
facebook.php
Mute
(Mute is the folder of the lib)
In facebook.php I try:
$app = new \Mute\Facebook\App();
Resulting in: Fatal error: Class 'Mute\Facebook\App' not found in...
The files are all in place.
I can say:
use \Mute\Facebook\App;
without getting any error/warning... What am I missing?
When I require the "App.php" via require_once I get the next error: Fatal error: Interface 'Mute\Facebook\Bases\AccessToken' not found in [...]/Mute/Facebook/App.php
Mute/Facebook/App.php:
<?php
namespace Mute\Facebook;
use Closure;
use Exception;
use Mute\Facebook\Bases\AccessToken;
use Mute\Facebook\Bases\Batchable;
use Mute\Facebook\Bases\Configurable;
use Mute\Facebook\Bases\Requestable;
use Mute\Facebook\Bases\RequestHandler;
use Mute\Facebook\Exception\CurlException;
use Mute\Facebook\Exception\GraphAPIException;
use Mute\Facebook\Exception\HTTPException;
use Mute\Facebook\Exception\InvalidArgumentException;
use Mute\Facebook\Exception\OAuthSignatureException;
use Mute\Facebook\Util;
class App implements AccessToken, Batchable, Configurable, Requestable, RequestHandler
{ [...]
function __autoload($class_name) {
require_once str_replace('\\', '/', $class_name) . '.php';
}
I created a __autoload function to "magically" require all files I need. Totally forgot about this method >.<

Using namespaces in Laravel 4

I'm new to Laravel and using PHP namespaces in general. I didn't run into any problems until I decided to make a model named File. How would I go about namespacing correctly so I can use my File model class?
The files are app/controllers/FilesController.php and app/models/File.php. I am trying to make a new File in FilesController.php.
Namespacing is pretty easy once you get that hang of it.
Take the following example:
app/models/File.php
namespace App\Models;
class File {
public function someMethodThatGetsFiles()
{
}
}
app/controllers/FileController.php
namespace App\Controllers;
use App\Models\File;
class FileController {
public function someMethod()
{
$file = new File();
}
}
Declare the Namespace:
namespace App\Controllers;
Remember, once you've put a class in a Namespace to access any of PHP's built in classes you need to call them from the Root Namespace. e.g: $stdClass = new stdClass(); will become $stdClass = new \stdClass(); (see the \)
"Import" other Namespaces:
use App\Models\File;
This Allows you to then use the File class without the Namespace prefix.
Alternatively you can just call:
$file = new App\Models\File();
But it's best practice to put it at the top in a use statement as you can then see all the file's dependencies without having to scan the code.
Once that's done you need to them run composer dump-autoload to update Composer's autoload function to take into account your newly added Classes.
Remember, if you want to access the FileController via a URL then you'll need to define a route and specify the full namespace like so:
Route::get('file', 'App\\Controllers\\FileController#someMethod');
Which will direct all GET /file requests to the controller's someMethod()
Take a look at the PHP documentation on Namespaces and Nettut's is always a good resource with this article
first, load your class with:
$ composer dump-autoload
then
$file = new File;
// your stuff like:
$file->name = 'thename';
$file->active = true;
$file->save();
Section: Insert, Update, Delete on Laravel 4 Eloquent's doc
To namespace your model, at the top of your model class right after the opening
Then when you call from controllers you will call new Whatever\Model;
You probably have to do a dump-autoload with composer the first time around.
have a look to it.. hopefully will clear your query....
<?php
namespace app\controllers;
use yii\web\Controller;
use app\models\users;
class UserController extends Controller{
public function actionIndex()
{
echo "working on .....";
}
}
Namespaces are defined at the top of PHP classes right after the opening php script tag like this:
<?php
namespace MyNameSpace;
When you then want to use the namespaced class in some other class, you define it like this:
new MyNameSpace\PhpClass;
or import it at the top of the file (after namespaces if present) like this:
<?php
//namespace
use MyNameSpace\MyPHPClass;
//then later on the code you can instantiate the class normally
$myphpclass = new MyPHPClass();
In Laravel namespaces can be defined anywhere composer can autoload them, I'd recommend defining namespaces within the app directory. So you can define a namespace like Utils for holding Utility classes by creating a Utils directory in the app directory, creating our utility classes and defining the namespace as we did above.
Afterwards you have run the command to ask composer to autoload classes:
$ composer dump-autoload

Categories