简体   繁体   中英

Symfony micro kernel and console component

I want to run console like in normal symfony app by php bin/console but configuration is difficult for me.

AppKernel.php

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
...

$loader = require __DIR__.'/../vendor/autoload.php';

AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

class AppKernel extends Kernel
{
    use MicroKernelTrait;

    public function registerBundles()
    {
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            ...
        );

        return $bundles;
    }

    protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
    {
        $loader->load(__DIR__.'/config/config.yml');
    }

    protected function configureRoutes(RouteCollectionBuilder $routes)
    {
        $routes->import(__DIR__.'/../src/App/Controller/', '/', 'annotation');
    }
    public function getCacheDir()
    {
        return __DIR__.'/../var/cache/'.$this->getEnvironment();
    }
    public function getLogDir()
    {
        return __DIR__.'/../var/logs';
    }
}

File structure:

├─ app/
|  ├─ AppKernel.php
│  ├─ config/
│  └─ Resources
|     └─ views
|        └─ micro
├─ src/
│  └─ App
|     └─ Controller
|        └─ MicroController.php
├─ var/
|  ├─ cache/
│  └─ logs/
├─ vendor/
│  └─ ...
├─ web/
|  └─ index.php
├─ composer.json
└─ composer.lock
└─ console

I add bin dir with console file like is in normal symfony but I get:

Fatal error: require(): Failed opening required 'C:\\xampp\\htdocs\\symfony-skeleton-micro-app/app/autoload.php' (include_path='C:\\xampp\\php\\PEAR') in C:\\xampp\\htdocs\\symfony-skeleton-micro-app\\console on line 16 PHP Warning: require(C:\\xampp\\htdocs\\symfony-skeleton-micro-app/app/autoload.php): failed to open stream: No such file or directory in C:\\xampp\\htdocs\\symfony-skeleton-micro-app\\console on line 16 PHP Fatal error: require(): Failed opening required 'C:\\xampp\\htdocs\\symfony-skeleton-micro-app/app/autoload.php' (include_path='C:\\xampp\\php\\PEAR') in C:\\xampp\\htdocs\\symfony-skeleton-micro-app\\console on line 16

console

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Debug\Debug;

// if you don't want to setup permissions the proper way, just uncomment the following PHP line
// read http://symfony.com/doc/current/setup.html#checking-symfony-application-configuration-and-setup
// for more information
//umask(0000);

set_time_limit(0);

/** @var Composer\Autoload\ClassLoader $loader */
$loader = require __DIR__.'/app/autoload.php';

$input = new ArgvInput();
$env = $input->getParameterOption(['--env', '-e'], getenv('SYMFONY_ENV') ?: 'dev');
$debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(['--no-debug', '']) && $env !== 'prod';

if ($debug) {
    Debug::enable();
}

$kernel = new AppKernel($env, $debug);
$application = new Application($kernel);
$application->run($input);

The solution:

First of all I removed this line $loader = require __DIR__.'/../vendor/autoload.php'; from AppKernel.php and create autoload.php file i app directory.

app/autoload.php

use Doctrine\Common\Annotations\AnnotationRegistry;
use Composer\Autoload\ClassLoader;

/** @var ClassLoader $loader */
$loader = require __DIR__.'/../vendor/autoload.php';

AnnotationRegistry::registerLoader([$loader, 'loadClass']);

return $loader;

Then I move console file to bin directory and make up composer.json file with classmap.

composer.json

{
    "require": {
        "symfony/symfony": "^3.3",
        "sensio/framework-extra-bundle": "^3.0",
        "doctrine/doctrine-bundle": "^1.6",
        "doctrine/orm": "^2.5"
    },
    "autoload": {
        "psr-4": {
            "": "src/"
        },
        "classmap": [ "app/AppKernel.php" ]
    }
}

Now we can run console by php bin/console

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM