【问题标题】:Runnning Symfony App Locally在本地运行 Symfony 应用程序
【发布时间】:2018-01-19 22:10:09
【问题描述】:

我刚刚开始了一份新工作,之前的 Web 开发人员使用 Symfony 框架创建了一个应用程序。我以前没有使用过 Symfony,但我对 Django 很熟悉,我注意到它们的结构非常相似。代码库托管在 gitlab 存储库中,我能够将 zip 文件下载到本地计算机上。 我开始阅读有关如何设置和安装 Symfony 应用程序的文档,但我注意到我下载的内容与“标准”Symfony 应用程序的外观之间存在许多差异。我的最终目标是在localhost 上运行这个应用程序来测试一些改进。

我开始调试我遇到的每一个问题,并解决了一些错误,但它们仍然不断出现。我将发布一些我认为与我的问题以及我的目录结构相关的文件。

编辑:清理后新建 Composer.json 文件。

composer.json:

   {
  "name": "symfony/framework-standard-edition",
  "license": "MIT",
  "type": "project",
  "description": "The \"Symfony Standard Edition\" distribution",
  "autoload": {
    "psr-4": {
      "AppBundle\\": "src/AppBundle"
    },
    "classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
  },
  "autoload-dev": {
    "psr-4": { "Tests\\": "tests/" },
    "files": [ "vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php" ]
  },
  "require": {
    "php": ">=5.5.9",
    "doctrine/doctrine-bundle": "^1.6",
    "doctrine/orm": "^2.5",
    "incenteev/composer-parameter-handler": "^2.0",
    "sensio/distribution-bundle": "^5.0.19",
    "sensio/framework-extra-bundle": "^5.0.0",
    "symfony/monolog-bundle": "^3.1.0",
    "symfony/polyfill-apcu": "^1.0",
    "symfony/swiftmailer-bundle": "^2.6.4",
    "symfony/symfony": "3.4.*",
    "twig/twig": "^1.0||^2.0"
  },
  "require-dev": {
    "sensio/generator-bundle": "^3.0",
    "symfony/phpunit-bridge": "^3.0"
  },
  "scripts": {
    "symfony-scripts": [
      "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
      "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
      "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
      "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
      "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
      "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
    ],
    "post-install-cmd": [
      "@symfony-scripts"
    ],
    "post-update-cmd": [
      "@symfony-scripts"
    ]
  },
  "config": {
    "platform": {
      "php": "5.5.9"
    },
    "sort-packages": true
  },
  "extra": {
    "symfony-app-dir": "app",
    "symfony-bin-dir": "bin",
    "symfony-var-dir": "var",
    "symfony-web-dir": "web",
    "symfony-tests-dir": "tests",
    "symfony-assets-install": "relative",
    "incenteev-parameters": {
      "file": "app/config/parameters.yml"
    },
    "branch-alias": {
      "dev-master": "3.4-dev"
    }
  }
}

我必须将 "files":["app/AppKernel.php"], 添加到作曲家以阻止错误是抱怨找不到内核。

AppKernel.php:

<?php

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
 {
  public function registerBundles()
  {
      $bundles = [
          new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
          new Symfony\Bundle\SecurityBundle\SecurityBundle(),
          new Symfony\Bundle\TwigBundle\TwigBundle(),
          new Symfony\Bundle\MonologBundle\MonologBundle(),
          new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
          new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new 
 Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
                new AppBundle\AppBundle(),
      ];

    if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
        $bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
        $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
        $bundles[] = new 
Sensio\Bundle\DistributionBundle\SensioDistributionBundle();

        if ('dev' === $this->getEnvironment()) {
            $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
            $bundles[] = new Symfony\Bundle\WebServerBundle\WebServerBundle();
        }
    }

    return $bundles;
}

public function getRootDir()
{
    return __DIR__;
}

public function getCacheDir()
{
    return dirname(__DIR__).'/var/cache/'.$this->getEnvironment();
}

public function getLogDir()
{
    return dirname(__DIR__).'/var/logs';
}

public function registerContainerConfiguration(LoaderInterface $loader)
{
    $loader->load(function (ContainerBuilder $container) {
        $container->setParameter('container.autowiring.strict_mode', true);
        $container->setParameter('container.dumper.inline_class_loader', true);

        $container->addObjectResource($this);
    });
    $loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
}
}

我现在遇到的主要问题是在运行php bin/console server:start 之后Fatal error: Class 'Symfony\Component\HttpKernel\Kernel' not found in /Users/.../app/AppKernel.php on line 7。这个应用程序内核文件甚至不在 repo 中,我不得不从标准 symfony 应用程序中复制它,这也解决了一些错误。

bin/console:

#!/usr/bin/env php
<?php


require __DIR__.'/../vendor/autoload.php';

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 https://symfony.com/doc/current/setup.html#checking-symfony-application-configuration-and-setup
// for more information
//umask(0000);

set_time_limit(0);

$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);
$kernel->loadClassCache();
$application = new Application($kernel);
$application->run($input);

这个文件也不存在,我也必须复制它,我还把use Symfony\Component\Debug\Debug; 注释掉了,因为它抛出了一个错误,说这个类不存在。

我知道这个问题非常开放,但基本上,我只是想知道如何能够在本地运行这个应用程序,我已经阅读了许多 github 问题线程、SO 问题和文档,但无济于事。

如果您需要任何其他信息/文件,请告诉我,我正在尝试在 Mac OSX 上运行。

编辑:最终能够创建一个干净的项目,但现在遇到了一个特殊问题。

运行php bin/console server:start 提供此错误:There are no commands defined in the "server" namespace. 我查看了此(server:run Exception There are no commands defined in the "server" namespace 但我的AppKernel.php 似乎与接受的答案中提供的匹配。

Edit2:bin/console list 的输出是:

Usage:
  command [options] [arguments]

Options:
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  help  Displays help for a command
  list  Lists commands

【问题讨论】:

  • 你应该尝试使用 symfony 安装程序以获得标准版本,这样它会设置整个事情并尽可能避免错误,我会推荐版本 2.8 和 ^3 并继续阅读symfony flex
  • @Ezekiel 如果我没记错的话,那将创建一个全新的应用程序?而且我需要使用现有项目,但它们不匹配,我尝试将相关文件夹复制到一个新的应用程序中,但也遇到了许多错误,因为我觉得该特定项目的配置文件已更改跨度>
  • 肯定会有配置改变,你只需复制之前的appkernel文件并替换为新的,你可以添加任何不存在的代码,然后复制之前的composer。 jsom 并运行 composer install,然后还有所有必要的模型、控制器和视图
  • 还有配置文件,比如哪里有防火墙和数据库设置
  • 下载项目,运行composer update并运行。

标签: php symfony


【解决方案1】:

使用标准的 Symfony 应用程序,您最起码需要的是:

  • 本地计算机上的 PHP
  • 可能是 MySQL 之类的数据库(有时 sqlite 可以作为简单的本地替代品)

您可以像以前一样克隆存储库或以 zip 格式下载源代码。使用终端转到您的项目文件夹并执行以下操作:

composer install

这应该会下载您的 composer.json 中描述的所有必需的依赖项。最后它可能会要求您提供一些参数,例如数据库设置。如果没有,请检查app/configs/parameters.yml.dist 并复制不带.dist 扩展名的文件。然后使用您喜欢的文本编辑器更新参数。

现在您应该准备好设置您的开发数据库了:

bin/console doctrine:database:create
bin/console doctrine:schema:create

这应该会创建一个空的数据库架构。您的项目也可能有固定装置和迁移。如有必要,只需使用bin/console list 检查命令。您还可以从生产数据库中获得(匿名的)数据库转储,您可以重复使用。

现在您只需要运行一个网络服务器。对于开发,Symfony 也有一个命令:

bin/console server:start

这将在后台启动服务器并保持运行。现在您应该可以使用http://localhost:8000在浏览器中打开该网站了

【讨论】:

  • 我运行了composer install 并添加了它要求我提供的参数,但我还没有查看数据库。现在将尝试运行命令,感谢您的帮助!
  • bin/console doctrine:database:create 这给了我同样的错误Class 'Symfony\Component\HttpKernel\Kernel' not found
  • 内核类应该由作曲家安装。您是否有供应商文件夹,您可以在其中搜索文件吗?
  • @dburmann 是的,我有一个供应商文件夹,快速通过后,我找不到文件。
  • 好的,我设法将 HttpKernel 类添加到 composer json 并安装它,现在我收到另一个错误'Symfony\Bundle\FrameworkBundle\Console\Application' not found
猜你喜欢
  • 2017-12-29
  • 1970-01-01
  • 2017-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-16
  • 2021-05-14
  • 2019-01-12
相关资源
最近更新 更多