Symfony console component, class not found - php

I'm using the console component without Symfony standard edition and I'm working on my application.php file. When I run it it says:
Fatal error: Class 'GenerateTableCommand' not found in D:\ConnectCommand\vendor\application.php on line 10
My code:
<?php
require __DIR__.'\autoload.php';
use Symfony\Component\Console\Application;
$application = new Application();
$application->add(new GenerateTableCommand());
$application->run();
?>
My repository can be found here if needed: https://github.com/guuse/ConnectCommand
Any help is greatly appreciated.

First of all there's no \GenerateTableCommand, as stated in the error message.
This class is under AppBundle\Command namespace, so it's full name is AppBundle\Command\GenerateTableCommand.
You should add use statement at the beggining:
use AppBundle\Command\GenerateTableCommand;
Anyway, you'll probably encounter further issues with loading this class, since you do not really have any autoloaders for your custom code, so PHP won't be able to load this class.
Also mixing 3rd party code with your own (in vendor directory) is not a good idea.

Related

Cannot redeclare class error in Laravel but works fine in native php

I am using Alchemy API for filter out some data. Everything works fine in the native code. But when i used it in my Laravel Controller it throws Cannot redeclare class. My controller,alchemyapi.php and example.php are in the same directory. Here is how i include alchemyapi.php in native code
<?php
require_once 'alchemyapi.php';
$alchemyapi = new AlchemyAPI("MY API KEY"); ?>
But when i include it in the controller it throws my the error. is there something i am missing ?
require_once 'alchemyapi.php';
$alchemyapi = new AlchemyAPI("MY API KEY");
The native code(example.php) works well without any issue. But in laravel controller it throws a error saying Cannot redeclare class AlchemyAPI
in alchemyapi.php line 20
Instead of using require_once use namespace in your alchemyapi.php and then use use for same namespace in your MyController
alchemyapi.php
<?php
namespace App\Http\Controller;
class AlchemyApi {
//your code
}
MyController.php
<?php
namespace App\Http\Controller;
use App\Http\Controller\AlchemyApi;
class MyController {
$alchemy = new AlchemyApi("Your_api_key");
}
OK, so what I think is happening is that alchemyapi.php is somehow already being included by the composer autoloader.
Try this.
Create the directory lib in the root of your project.
Move alchemiapi.php into lib.
Under the "autoload" section in your composer.json add the following code. Make sure the JSON is valid:
"files": [
"lib/alchemyapi.php",
],
Run composer dump-autoload. If it errors, your composer.json is invalid or you haven't put the file in the correct place.
Delete require_once 'alchemyapi.php'; from your controller.
When dealing with composer, this is how you deal with classes in the global namespace. After running composer it scan those directories in app for PSR-4 classes.
I can't be sure but I think that composer is looking for it and you are also manually requiring it. That would explain why PHP thinks you are redeclaring the class.

PHP Framework Zend not Working

When I try to load it some error occurs that prevents the rest of the page from loading.
My file basically looks like this:
use Zend\Form\Form;
include 'Zend/Form/Form.php';
$form = new Form();
// Page Content
What am I doing wrong?
PHP Errors:
Fatal error: Class 'Zend\Form\Fieldset' not found in /Applications/MAMP/htdocs/wordpress/wp-content/plugins/contest-guess-image/Zend/Form/Form.php on line 24
You can't use ZF2 without autoloading. Composer makes this easy, if you're not using composer you'll need to setup the ZF2 standard autoloader yourself before using any other ZF2 classes.

Autoloader issue with Zend

I'm new to Zend and am currently trying to use Swift Mailer as my messaging system. However, I keep getting this error:
Fatal error: Class 'Adviser\Controller\Swift_Message' not found
I did some research and thought it might be an autoloader issue. So I added the following two lines:
$autoloader = new StandardAutoloader();
$autoloader->registerNamespace('Swift_','/Applications/MAMP/htdocs/zsa/Swift-4.3.0/lib/classes/Swift');
I'm still getting the error. I've also set the path accordingly to "swift_required.php"
require_once '/Applications/MAMP/htdocs/zsa/Swift-4.3.0/lib/swift_required.php';
Any thoughts on how to fix this?
It looks like Swift is not namespaced, so you should use $autoloader->registerPrefix instead if available.
Using http://getcomposer.org/ is a good option too.

Class 'Zend_Search_Lucene' not found

A big beginner of Zend-framework on PHP calls, I could include it on Netbeans IDE. Now I'm trying to use it to achieve a Lucene indexer and searcher using Zend_Lucene, I followed the getting started of the official site, unfortunately they explain the whole thing with just few words. Anyway, I copied pasted this $index = Zend_Search_Lucene::create($indexPath);, but I got a message onto this line saying: Fatal error: Class 'Zend_Search_Lucene' not found in C:\wamp\www\witswork\luceneTry.php
means that the function still unknown, maybe, some files need to be copied on my project folder but really I'm running out of ideas right now.
Accept my regards,
dany90.
You need to load the php file which contains the Zend_Search_Lucene class first. One option is to load your/path/to/library/Zend/Search/Lucene.php:
require_once 'my/path/to/library/Zend/Search/Lucene.php';
$index = new Zend_Search_Lucene::create($indexPath);
This class loads all its dependencies, so you don't need to worry about that.
Another option is to use the autoloader of Zend, Zend_Loader_Autoloader. This class is a singleton and registers itself with spl_autoload() when you retrieve it for the first time:
$autoloader = Zend_Loader_Autoloader::getInstance();
$index = new Zend_Search_Lucene::create($indexPath);
After the autoloader is loaded, you just can use Zend_Search_Lucene without the require_once() call. In the manual of Zend Framework you can find more information about the autoloader.

Using PEAR libraries in custom Magento modules produces "Failed opening required..." error

I have written a Magento module to listen for the "OrderSave" event and perform some API calls to a third party application. Most of the functionality is working great, but I needed to handle an XML response from the API and when I tried to use the PEAR XML_Unserializer class I received the following error:
Fatal error: require_once() [function.require]: Failed opening required 'XML/Parser.php' (include_path='/Users/jeremymoore/Sites/Helm/html/app/code/local:/Users/jeremymoore/Sites/Helm/html/app/code/community:/Users/jeremymoore/Sites/Helm/html/app/code/core:/Users/jeremymoore/Sites/Helm/html/lib:.:/Applications/MAMP/bin/php5/lib/php:/usr/loca/zend//share/ZendFramework/library') in /Users/jeremymoore/Sites/Helm/html/lib/PEAR/XML/Unserializer.php on line 58
My module has an Observer.php file in the model which looks starts as follows:
<?php
require_once 'lib/PEAR/XML/Serializer.php';
require_once 'lib/PEAR/XML/Unserializer.php';
require_once 'lib/Pest/PestXML.php';
Zend_Loader::registerAutoload();
class Helm_Litmos_Model_Observer
{
public function hookToOrderSaveEvent()
{
//Do API Stuff Here
}
}
The hookToOrderSaveEvent functon creates new instances of the serializer and unserializer classes. Before I added the Unserializer code, I had everything working serializing objects and making API calls. It seems that things break down when the XML_Unserializer class tries to reference Parser.php.
I'm not sure that the "require_once" method I'm using here is the appropriate way for me to include these libraries. I'm using Magento 1.4.1.1 which is currently being used on my local machine running MAMP.
Any suggestions on a better way to autoload or include these libraries for use in my module or just ideas on how to fix what I have would be appreciated.
Thanks
You can try and use Mage::getBaseDir('lib')
require_once Mage::getBaseDir('lib').'/PEAR/XML/Serializer.php';
Alan has a good article on Magento's base directories:
http://alanstorm.com/magento_base_directories

Categories