Symfony微内核和控制台组件

时间:2017-07-18 11:04:34

标签: php symfony

我想在php bin/console的普通symfony应用程序中运行控制台,但配置对我来说很难。

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';
    }
}

文件结构:

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

我在控制台文件中添加bin目录,就像在正常的symfony中一样,但是我得到了:

  

致命错误:require():无法打开所需的'C:\ xampp \ htdocs \ symfony-skeleton-micro-app / app / autoload.php'(include_path ='C:\ xampp \ php \ PEAR')in第16行的C:\ xampp \ htdocs \ symfony-skeleton-micro-app \ console   PHP警告:require(C:\ xampp \ htdocs \ symfony-skeleton-micro-app / app / autoload.php):无法打开流:C:\ xampp \ htdocs \ symfony-skeleton-micro中没有这样的文件或目录第16行的-app \ console   PHP致命错误:require():在C中打开所需的'C:\ xampp \ htdocs \ symfony-skeleton-micro-app / app / autoload.php'(include_path ='C:\ xampp \ php \ PEAR')失败:第16行\ xampp \ htdocs \ symfony-skeleton-micro-app \ 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);

1 个答案:

答案 0 :(得分:0)

解决方案:

首先,我从AppKernel.php中删除了这行$loader = require __DIR__.'/../vendor/autoload.php';并创建了autoload.php文件i 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;

然后我将控制台文件移动到bin目录,并用classmap组成composer.json文件。

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" ]
    }
}

现在我们可以通过php bin/console

运行控制台了