"Convert" package to load in Symfony - php

I have THIS package (a payment gateway), which I would like to use in Symfony 3.0.1
Unfortunately I get this error:
ClassNotFoundException in AppKernel.php line 21: Attempted to load class "SofortBundle" from namespace "Sofort\SofortLib".
Did you forget a "use" statement for another namespace?
In the sofort\sofortlib-php folder i created the file SofortBundle.php with this content
<?php
namespace Sofort\SofortLib;
use Symfony\Component\HttpKernel\Bundle\Bundle as BaseBundle;
class SofortBundle extends BaseBundle
{
}
and I loaded the Bundle in AppKernel.php:
new Sofort\SofortLib\SofortBundle(),
But that only leads to above exception.
What am I missing?

Don't copy packages to your custom folder. Install package as described:
In composer.json add:
"require": {
"sofort/sofortlib-php": "3.*"
}
Run composer update sofort/sofortlib-php
In your code you can use the library like this:
use \Sofort\SofortLib\Billcode;
class MyClass
{
function doSomething($configkey) {
$SofortLibBillcode = new Billcode($configkey);
...
}
}

Related

PHP how to include composer autoload in Class file

I got difficult to include composer autoload in Class file, it not working on require_once('../vendor/autoload.php');
require_once('phpmailer/PHPMailerAutoload.php');
require_once('../vendor/autoload.php');
class Test {
function X()
{ ... }
}
What is the proper way to load multiple include files in a class?
If you (correctly) use composer you need do add only the vendor autoload file. Then add the other dependency via composer vendor library or add custom path (composer do the rest for you).
As example, more simply:
start in an empty directory
launch the command:
php composer.phar init
Add the dependency of the library in the composer.json files (if you don't add it in the init process) with the command (suggested by the packagist site)
composer require phpmailer/phpmailer
Then your class should be like:
require_once('../vendor/autoload.php');
class Test {
function X()
{ ... }
}
Hope this help
I think you want something like this
class Loader
{
public function __construct()
{
require_once('phpmailer/PHPMailerAutoload.php');
require_once('../vendor/autoload.php');
}
}
$loader = new Loader();
just add some function as you want
tell me if this help you ... goodluck

PHP - Doctrine - How to autoload entities classes using a single namespace

I have a group of PHP files containing classes (entities). Each class has the same namespace:
// src/App/Entity/Actions.php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Actions
*
* #ORM\Entity
*/
class Actions
{
// SOME CODE
I autoload the PHP files containing the classes with composer:
"autoload": {
"psr-0": {
"App": "src/"
}
}
And in my bootstrap.php file, I add this line:
use App\Entity;
So I figured that because told the app to use the App\Entity namespace, that I can just call the entity classes like this: $entity = new Actions();
but when I try that, I get this error:
Fatal error: Class 'Actions' not found in C:\wamp64\www\spider\chebi2\inc\orm_tools.php on line 49
If I do this:
use App\Entity; use App\Repository;
if (class_exists('Actions')) { dump('exists'); } else { dump('not exists'); }
if (class_exists('\App\Entity\Actions')) { dump('exists'); } else { dump('not exists'); }
Heres what it outputs:
PS C:\wamp64\www\spider\chebi2> php .\get_actions.php
"not exists"
"exists"
So it can only find the class when I provide the full namespace. And weirdly enough, when I tried this:
// Direct path to the Actions.php file
use App\Entity\Actions;
if (class_exists('Actions')) { dump('exists'); }
else { dump('not exists'); }
if (class_exists('\App\Entity\Actions')) { dump('exists'); }
else { dump('not exists'); }
I get the same result:
PS C:\wamp64\www\spider\chebi2> php .\get_actions.php
"not exists"
"exists"
So now I'm even more confused. What is the point in using: use App\Entity; if it doesn't actually make the classes in that namespace directly available? And why is assigning the direct path to the class use App\Entity\Actions; not even working?
Am I doing something wrong here? Is there a correct way to use namespaces that I'm not understanding?
PSR-0 is depracated you should use PSR-4
in PSR-4
composer.json
"autoload": {
"psr-4": {
"App\\": "src/",
}
}
in directory src/ which is on same level as composer.json add directory Entity so in path src/Entity add class file Actions
namespace App\Entity;
class Actions
{
}
you can also use composer dump-autoload and check vendor/composer/autoload* fiels and see if namespaces are registered there.'
Regarding class_exists() it does not work with short names or aliases you need to provide the full name of class. I'd suggest using ::class operator So in your case it would be:
<?php
use App\Entity\Actions;
class_exists(Actions::class);
Thanks! I changed the auto loader to psr-4, and attached it to this:
"psr-4": {
"App\\": "src/"
}
dump-autoload is exactly what I was looking for, but I don't see any included files or classes listed:
PS C:\wamp64\www\spider\chebi2> composer dump-autoload -vvv
Reading ./composer.json
Loading config file ./composer.json
Checked CA file C:\Users\horse\AppData\Local\Temp\composer-cacert-12fdaece071ee9515fa28aabed5ab089876ae257833106e15a583e060eaff6b5.pem: valid
Executing command (C:\wamp64\www\spider\chebi2): git branch --no-color --no-abbrev -v
Executing command (C:\wamp64\www\spider\chebi2): git describe --exact-match --tags
Executing command (C:\wamp64\www\spider\chebi2): git log --pretty="%H" -n1 HEAD
Reading C:/Users/horse/AppData/Roaming/Composer/composer.json
Loading config file C:/Users/horse/AppData/Roaming/Composer/composer.json
Reading C:\wamp64\www\spider\chebi2/vendor/composer/installed.json
Reading C:/Users/horse/AppData/Roaming/Composer/vendor/composer/installed.json
Running 1.2.2 (2016-11-03 17:43:15) with PHP 5.6.25 on Windows NT / 10.0
Generating autoload file
I still can't find the entity classes.
To clarify, I should have the folder structure like this:
- src (contains only subdirectories)
- Entity (contains the entity files)
- Repositories
- App (empty)

Composer autoload custom classes

I try to autoload my custom pdo class with composer.
Ran the following command to update autoload:
compser update
composer install
Both seem to work, no error prompted. But,
vendor/composer/autoload_namespaces.php
Does not list the custom namespace added to composer.js.
File structure
-Root
->classes
->pdo
->class.php
->vendor
->various extensions loaded with composer
index.php
PHP Class
namespace Classes\Pdo;
Class DB {
//Do some stuff...
}
Composer.js
"autoload": {
"psr-4": {
"Classes\\Pdo\\": "classes/pdo"
}
}
Index.php
$pdo = new \Classes\Pdo\DB(); //Fatal error: Class 'Classes\Pdo\DB' not found
Old question, but I just ran across this myself.
For future Googlers, in my case the issue turned out to be the name of the class file did not exactly match the class name.
See this post: Why does 'composer dumpautoload -o' fix 'Class not found' PHP error?

phpspec creates files in wrong folders per psr-4 namespace specification

I scrapped the earlier form of my question because it was too convoluted. Here's the new version.
I want to use phpspec with my psr-4 formatted projects.
Here's the way I tried to set up a test project:
Created a new folder for the project:
cd ~/Desktop/
mkdir TestPhpSpec
cd TestPhpSpec
create a new composer.json file and require phpspec:
composer require phpspec/phpspec
Which creates my composer.json file:
{
"require": {
"phpspec/phpspec": "^2.3"
}
}
I add my psr-4 namespace to the autoload property of my composer.json file:
{
"require": {
"phpspec/phpspec": "^2.3"
},
"autoload": {
"psr-4": {
"Acme\\": "src/Acme"
}
}
}
Then I dump my autoload to make sure my namespace is loaded: composer dumpautoload
After that, I create my phpspec.yml to describe the namespace to phpspec:
suites:
acme_suite:
namespace: Acme
psr4_prefix: Acme
Then I describe the class I want to start building:
phpspec describe Acme/Markdown
This is where I run into the first problem. Even though I specify the Acme namespace in my describe command, the spec does not get placed in a folder matching the namespace:
Though the class it creates is namespaced correctly:
<?php
namespace spec\Acme; // correct namespace
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class MarkdownSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Acme\Markdown');
}
}
Then if I try to run the test to start TDD-ing.
phpspec run
It offers to create the class for me and I let it. From there I get the second problem; I get the error message:
[PhpSpec\Process\Prerequisites\PrerequisiteFailedException]
The type Acme\Markdown was generated but could not be loaded. Do you need to configure an autoloader?
And the class it creates is not in it's namespaced folder:
The class it creates is also namespaced correctly:
<?php
namespace Acme; // correct namespace
class Markdown
{
}
I've looked over the docs and can't figure out what I'm doing wrong. Any suggestions?
Try with
suites:
acme_suite:
src_path: Acme/src
spec_path: Acme/spec

Composer and PSR-0 class autoloading with namespaces

Hello guys i have problem with autoloading my class with composer. On Linux all work perfect, but now my boss change env and set Windows. All this work on linux but windows show newbie fatal error:
Fatal error: Class 'AbstractController' not found in
D:\xampp\htdocs\ikacFw\frontController.php on line 7
Common to see my composer.json and stucture for better picture on problem.
Stucture is :
frontController.php
-- vendor
----- Doctrine
----- Ikac
--------- Components
---------- Mvc
------------- Controller
Am trying to load all data from vendor directory.
Composer.json
{
"autoload": {
"psr-0": {
"vendor": ""
}
}
}
Also new component i add manual. Like this :
$loader = require_once 'vendor/autoload.php';
$loader->add('vendor', "Ikac");
Okay next when i try to call :
<?php
require_once 'vendor/autoload.php';
use Ikac\Mvc\Controller;
$a = new AbstractController();
I get error "not found".
My class AbstractController contain defined namespace but dont work again. Like test i do this:
<?php
//vendor/Ikac/Mvc/Controller/AbstractController.php
namespace Ikac\Mvc\Controller;
class AbstractController {
function __construct() {
echo __CLASS__;
}
}
?>
I do from cmd composer dump-autoload, install, but dont work. All this perfect work on linux but here wont. Any idea how to fix this or where i do mistake.
Thanks guys!
SLOVED:
{
"autoload": {
"psr-0": {
"": "vendor/"
}
}
}
Well you should do
<?php
require_once 'vendor/autoload.php';
use Ikac\Mvc\Controller\AbstractController;
$a = new AbstractController();
Your autoloading declaration is wrong.
You will NEVER ever need to include the vendor folder in any autoloading. The vendor folder will contain the autoloading for both all dependencies, and - if configured - for your own classes as well.
You can use Composer to create autoloading for your own classes. Just include the correct info. But from your current info I cannot deduct what would be correct.

Categories