Phalcon autoloader is configured in the following way:
$loader = new Phalcon\Loader;
$loader->registerDirs(array(
'../app/4/controllers/',
'../app/models/',
'../app/services',
'../app/forms/',
'../app/vendor/'
))->register();
The directory tree is:
app
├───services
│ ├─── PayPalService.php - important
└───vendor
└───PayPal
├───Api
│ └───Amount.php
│ └───and all other files
├───Auth
├───Common
├───Converter
├───Core
├───Exception
├───Handler
├───Rest
├───Security
├───Transport
└───Validation
At the beginning of PalPalService.php there is:
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
use PayPal\Rest\ApiContext;
Phalcon autoloader apparently can't find these classes so I get error Undefined class constant 'PayPal'. For other classes that don't use namespaces this autoloader works. What is wrong? How to use PayPal SDK without Composer? I think Phalcon's autoloader should deal with it.
I also tried:
$loader->registerNamespaces(
array(
"PayPal" => "../app/vendor/PayPal/"
)
);
It still doesn't find PayPal classes. I don't want to list each PayPal directory because according to docs Phalcon's autoloader should change namespace separator into directory separator.
"../app/vendor/PayPal/" should be "../app/vendor/"
$loader->registerNamespaces(
array(
"PayPal" => "../app/vendor/"
)
);
Related
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.
I am trying to use Azure storage for php, the setup steps are to include the namespace, include composer autoload and then use the azure classes, so I have the following. However further down I use the class Microgrid and it's not found because of the namespace, it is in a different directory. How can I use other classes that aren't part of that namespace? Also, what's the correct way to specify your namespace path? It's in a different directory relative to the page this is for and the one I am using is not at the root, should I start at the root?
namespace MicrosoftAzure\Storage;
use \MicrosoftAzure\Storage\Common\ServicesBuilder;
use \MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions;
use \MicrosoftAzure\Storage\Blob\Models\PublicAccessType;
use \MicrosoftAzure\Storage\Common\ServiceException;
require_once '/var/www/html/vendor/autoload.php';
$action = MicroGrid::GetParameter('action');
ClassNames are considered relative to the current namespace unless they start with a \
This means that inside the MicrosoftAzure\Storage namespace you can use the relative namespace for class.
If you want to call a class from a different namespace you should call fully-qualified name for it like
$action = \MicrosoftAzure\WhereEver\MicroGrid::GetParameter('action');
or use the name space or unique class with fully-qualified name
use \MicrosoftAzure\WhereEver;
or
use \MicrosoftAzure\WhereEver\MicroGrid;
then:
$action = MicroGrid::GetParameter('action');
Edited to make it clear
namespaces allow us to avoid naming collisions and to alias long
names caused by avoiding naming collisions.
it depends to your autoloader for a simple example I create a new project and make this autoloader in the index.php located at root directory
function __autoload($className){
//if it's a full name with windows style slashes correct the path
$file_name = str_replace('\\', '/', $className);
require_once("vender/src/".$file_name.".php");
}
when I call $date = new \App\Utility\Date(); autoloader will require this file:
verdor/src/App/Utility/Date.php
and in Date.php I used this name space namespace App\Utility;
PHP does not provides a class autoloader out of the box.
There are many autoloaders for PHP, and the most common autoloader standard is the PSR-4, used by many frameworks and applications.
If you are not using an autoloader, you should require every class file (and recursive dependencies) before using it.
Azure uses Composer Autoloader and PSR-4.
You should use Composer Autoloader on your project, then import your class from the right namespace (you are not importing it on your example code)
A namespace basically groups your classes together. You can use something outside your namespace by explicitly using it e.g.
namespace App;
use Illuminate\Database\Eloquent\Model;
In this case I'm 'grouping' this and other classes in a namespace called 'App' but I want to use 'Model' provided by Eloquent (the Eloquent class having a namespace of 'Illuminate\Database\Eloquent') in this class.
If 'Microgrid' is not part of your current namespace, you'll need to explicitly add its namespace in your 'use' statements.
I have installed elasticsearch using composer. This is my AppKernel.php file
new Elasticsearch\Client()
This is my TestController.php file.
<?php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Elasticsearch\Client;
//use Elasticsearch\Common\AbstractFactory;
class TestController extends Controller{
/**
* #Route("/", name="test-homepage")
*/
public function indexAction(){
$client = new Elasticsearch\Client();
dump($client);
die;
}
}
I am using eclipse as my ide and it shows me error like elasticsearch\Client cannot be resolved.Why is this not working?
First of all: If you define use statement then you don't need FQCN. BTW your FQCN is not right it should starts with \ to prevent loading class from current namespace.
Then: in AppKernel.php you need to define bundles, not every library you installed.
If you have small experience in PHP try to use more easy-to-study frameworks. Symfony is mostly for experienced developers.
If you think that you can work with Symfony then I would recommend you bundle for integrating ElasticSearch and Elastica: https://github.com/FriendsOfSymfony/FOSElasticaBundle. It will save your time.
You should try to instanciate the imported class, using the short name (base on the use statements on your class), like this :
$client = new Client();
Or with the FQCN :
$client = new \Elasticsearch\Client();
(note that the new statement begin with a \ which prevent from trying to load the class from the current namespace (ie : AppBundle\Controller : \AppBundle\Controller\Elasticsearch\Client)
a quick (maybe stupid) question.
I'm using a namespace for my controllers, like so:
namespace Members;
use DB;
use Input;
use PerformanceReport;
use Redirect;
class AdminController extends MembersController {
And as expected, I have to provide use statements for the Laravel classes I wish to use.
From what I've understood, the composer autoloader prevents this from being necessary if used correctly.
So my question being, is it possible to configure the autoloader to suit my needs, and if so, how would I go by doing this?
Your question is connected with the way PHP namespaces work, not with composer's autoloader.
If your class is in namespace Controllers; and you'd write Redirect::to('/') php would assume that the class you're referring to is in the current declared namespace (in that case Controllers/Redirect). You can either write \Redirect::to('/') or put a use Redirect statement on top like you did.
Composer's autoload just maps namespaces to their file directory (see vendor/composer/autoload_classmap.php for how it maps it).
If you want to dive more into composer's autoloading, i'd recommend read up on PSR-0 and PSR-4.
I'm currently working on a Symfony2 project with Phpspec and I'm having problems to extend a Spec class described in a different namespace.
In my project, for instance, I'm having the following class described in spec/Acme/Model/Foo/FooSpec.php :
namespace spec\Acme\Model\Foo;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
abstract class FooSpec extends ObjectBehavior{
//some code here
}
And I have another class in spec/Acme/Model/Bar/BarSpec.php extending FooSpec :
namespace spec\Acme\Model\Bar;
use spec\Acme\Model\Foo\FooSpec;
class BarSpec extends FooSpec{
//some code here
}
When I try to run phpspec, I have the following error :
PHP Fatal error: Class 'spec\Acme\Model\Foo\FooSpec' not found in /home/user/Projects/Acme/spec/Acme/Model/Bar/BarSpec.php on line 9
The only way I found to make it work was to add the following line in spec/Acme/Model/Bar/BarSpec.php:
include('./spec/Acme/Model/Foo/FooSpec.php');
I don't know why I have to include this specific file to make it run, especially when the other classes (like PhpSpec\ObjectBehavior) are correctly found.
Do you have any idea why is this happening?
Edit:
As suggested by #Phil and #Sheikh Heera in the comments, I tried to set up an autoload to register my spec namespace but it's not working neither. Here is what I tried so far :
require_once getcwd() . '/vendor/composer/ClassLoader.php';
$loader = new \Composer\Autoload\ClassLoader();
// register classes with namespaces
$loader->add('spec', getcwd().'/spec');
// activate the autoloader
$loader->register();
I also tried to modify the file vendor/composer/autoload_namespaces.php to add this :
return array(
//some code here
'spec' => array(getcwd() . '/spec'),
//some more code
);
But still the same error. I also tried with 'spec' => array(getcwd()) or $loader->add('spec', getcwd()); just to see what will happen and this time I get a Cannot redeclare class on another spec class.
My php version is PHP 5.4.9-4ubuntu2.4 (cli).
Thank you in advance for your help.
A more generic solution is to put the spec directory into the autoload-dev configuration of your composer.json:
"autoload-dev": {
"psr-0": {
"spec\\":""
}
},
This way, composer will generate the namespace also for the specs, which should be on the root of the repository:
return array(
'spec\\' => array($baseDir . '/'),
...
);
Finally, what I actually did to "solve" my "problem", as suggested by Phil and Sheikh Heera in the comments, is to autoload my namespace so it is recognized by spec.
Here is what I did to make it work :
I added the following line to the file `vendor/composer/autoload_namespaces.php` :
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
//some code
'spec\\Acme\\Behavior' => array(getcwd()),
//some code
);
Then I created the following folder structure :
│── spec
│ └── Acme
│ └── Behavior
│ └── FooBehavior.php
I declared the namespace in `spec/Acme/Behavior/FooBehavior.php` this way :
namespace spec\Acme\Behavior;
And used it in `spec/Acme/Model/Bar/BarSpec.php` as following :
use spec\Acme\Behavior\FooBehavior;
class BarSpec extends FooBehavior{
//...
}
I know this is not the best practice because everytime someone will be working on this project it will have to reproduce manually the first step. So if you have any better idea, please feel free to comment or post an answer.