I followed Symfony2 create a basic command line.
<?php
// application.php
use Acme\Command\GreetCommand;
use Symfony\Component\Console\Application;
$application = new Application();
$application->add(new GreetCommand);
$application->run();
But I received the faltal Error:
Fatal error: Class 'Symfony\Component\Console\Application' not found
According to http://symfony.com/doc/current/components/using_components.html, you need to include the autoloader at the top of your PHP script.
// update this to the path to the "vendor/"
// directory, relative to this file
require_once __DIR__.'/../vendor/autoload.php';
Related
I'm quite new to using composer and I am not too sure what I am doing at this point. I am currently trying to use this Nmap library I found. now once I have this library installed using this commandcomposer require willdurand/nmap
I created a index.php file with
<?php
require __DIR__ . '/vendor/autoload.php';
$hosts = Nmap::create()->scan([ 'example.com' ]);
$ports = $hosts->getOpenPorts();
echo $ports;
?>
This is what my composer.json file
{
"require": {
"willdurand/nmap": "^0.5.0"
}
}
When I run this I get PHP Fatal error: Uncaught Error: Class 'Nmap' not found in /var/www/html/nmap.php:5. I have Nmap installed on my Unix system. Any help on this issue would be great.
When you do not define a current namespace, PHP looks for any references classes in the root namespace. However, it cannot find Nmap in the root namespace because it is defined in the ´Nmap´ namespace.
You have to either add the namespace to the class defenition:
$hosts = \Nmap\Nmap::create()->scan([ 'example.com' ]);
Or, add a using statement for this class at the top of your file: (under <?php ofcourse)
use Nmap\Nmap;
I've been trying to use this Anti XSS Library: https://github.com/voku/anti-xss/blob/master/README.md
But I'm getting following error:
AH01071: Got error 'PHP message: PHP Fatal error: Class 'voku\\helper\\AntiXSS' not found in /var/www/vhosts/example.com/httpdocs/xss.php on line 6\n'
Here's the code I'm using:
<?php
use voku\helper\AntiXSS;
require_once __DIR__ . '/vendor/autoload.php';
$antiXss = new AntiXSS();
$harm_string = "Hello, i try to <script>alert('Hack');</script> your site";
echo $antiXss->xss_clean($harm_string);
?>
You need to require the autoload before you reference the class.
require_once __DIR__ . '/vendor/autoload.php';
use voku\helper\AntiXSS;
Otherwise, this dependancy namespace will not be loaded into memory in the context, as it is loaded inside the autoload.php file. This is why it says class not found.
Also Make Sure You've Installed via "composer require"
composer require voku/anti-xss
I am trying to include a package from composer, but I am receiving a class not found error.
I have tried the below possibilities.
$supermeteor = new \Supermeteor\Supermeteor('XXXXXXXX');
and
use Supermeteor\Supermeteor;
$supermeteor = new Supermeteor('xxxxxxxx');
Packages composer.json:
"psr-4": {
"Supermeteor\\": ""
}
Packages namespace :
namespace Supermeteor;
Packages class name :
class Supermeteor() {}
Error message
Uncaught Error: Class 'Supermeteor\Supermeteor' not found in
C:\path\to\my\file.php:16
I just tested your package locally, and it seems to work fine for me using the same code as you provided in your question. This is how I tested it.
1. Create a new project
Create a new directory on your computer.
2. Add the package to a new project using Composer
Locate your new directory on the command line and add the package to your projects autoloader by running the below composer command.
composer require supermeteor/sdk-php
3. Use the package
Create an index.php file in the same directory as your composer.json and add the below code.
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Supermeteor\Supermeteor;
$supermeteor = new Supermeteor("xxx");
4. Test the results
In the terminal window start a new php server to serve your project.
php -S localhost:8089
Now access the site via your browser at http://localhost:8089.
How can I get a service from the command class? I have been trying to use ContainerAwareCommand as the parent of my command class, and then just using $this->getContainer()->get('my_service'), but when I run the command in cli, I get the following error:
Call to undefined method Symfony\Component\Console\Application::getKernel()
inside the getContainer() method of the ContainerAwareCommand class.
The file through which I run the command is:
<?php
require_once __DIR__.'/../vendor/autoload.php';
require_once __DIR__.'/AppKernel.php';
use AppBundle\Console\Command\ChangeEmailCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Debug\Debug;
$input = new ArgvInput();
$env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev');
$debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(array('--no-debug', '')) && $env !== 'prod';
if ($debug) {
Debug::enable();
}
$kernel = new AppKernel($env, $debug);
$application = new Application($kernel);
$application->add(new ChangeEmailCommand());
$application->run();
You have to use the Application of the FrameworkBundle instead of the Console component. This class extends the one in the Console component, but adds awareness of the Kernel and the Container. Your Application version doesn't have this awareness (as it's designed for standalone use, outside of the Symfony context), resulting in a "method getKernel() does not exists".
So change this:
use Symfony\Component\Console\Application;
To:
use Symfony\Bundle\FrameworkBundle\Console\Application;
Btw, normally you won't need to create this file yourself. You can just use app/console in the Standard Edition: https://github.com/symfony/symfony-standard/blob/2.7/app/console when you installed Symfony using the installer/composer create-project.
I added to my project FOSUserBundle, on localhost it's works fine. But on web server I get
Fatal error: Class 'FOS\UserBundle\FOSUserBundle' not found in
/home/zone24/domains/zone24.linuxpl.info/public_html/worldclock/app/AppKernel.php on line 22
I can't cache:clear because I get this same message.
My autoload.php
<?php
use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = require __DIR__.'/../vendor/autoload.php';
// intl
if (!function_exists('intl_get_error_code')) {
require_once __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';
$loader->add('', __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs');
}
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
return $loader;
The line from AppKernel.php who make mistake
new FOS\UserBundle\FOSUserBundle(),
Folderfriendsofsymfony in /vendor has 775 permisions
Are you using APC ? If yes, restart apache to clear its cache.
If that does not help, you can always force the autoloader to register a specific namespace with $loader->add(), but you should not have to do that. FOS works fine for me without adding that.