标签: symfony4

symfony:如何为不同的环境设置配置参数文件?

如何为每个环境设置不同的配置参数文件?

目前,参数parameters.yml都用于环境devprod环境,但我需要不同的参数才能在prod中部署我的应用程序.

symfony symfony-2.8 symfony-3.4 symfony4

7
推荐指数
1
解决办法
2409
查看次数

使用Api-Platform,如何在创建时自动将授权用户添加为资源所有者?

如何在创建时自动将当前授权用户添加到资源 ( POST)。

我正在使用 JWT 身份验证,并且 /api/ 路由受到未授权用户的保护。我想设置它,以便当经过身份验证的用户创建新资源(即通过向 发送POST请求/api/articles)时,新创建的Article资源与经过身份验证的用户相关。

我目前正在使用EventSubscriber每个资源类型的自定义来从令牌存储中添加用户。

这是订阅者基类的要点:https : //gist.github.com/dsuurlant/5988f90e757b41454ce52050fd502273

以及扩展它的实体订阅者:https : //gist.github.com/dsuurlant/a8af7e6922679f45b818ec4ddad36286

但是,例如,如果实体构造函数需要用户作为参数,则这不起作用。

例如

class Book {

    public User $owner;
    public string $name;

    public class __construct(User $user, string $name) {
        $this->owner = $user;
        $this->name  = $name;
    }
}
Run Code Online (Sandbox Code Playgroud)

如何在实体创建时自动注入授权用户?

php symfony api-platform.com symfony4

7
推荐指数
1
解决办法
1337
查看次数

Symfony4:Doctrine2工作但PHPUnit测试中没有连接(内核启动)

Symfony4中的奇怪问题:Doctrine有效,我可以使用验证模式,创建数据库等php bin/console doctrine:schema:create.但我的PHPUnit测试没有连接.通过运行./bin/phpunit我得到SQLSTATE[HY000] [2002] No such file or directory例外.

我按照文档中有关启动内核的步骤进行操作:http: //symfony.com/doc/current/testing/doctrine.html

我的代码:

class PersistingResultTest extends KernelTestCase
{
    protected $em;

    protected function setUp()
    {
        $kernel = self::bootKernel();

        $this->em = $kernel->getContainer()
            ->get('doctrine')
            ->getManager();
    }

    public function testPersistingLogFile()
    {
        // Just a simple schema command to test connection
        $tool = new SchemaTool($this->em);
        $tool->createSchema($this->em->getMetadataFactory()->getAllMetadata());
    }


   protected function tearDown()
    {
        parent::tearDown();

        $this->em->close();
        $this->em = null; // avoid memory leaks
    }
}
Run Code Online (Sandbox Code Playgroud)

谁知道为什么会这样?一个bug?

编辑:

显然.env文件读取不正确.我将DATABASE_URL …

php symfony doctrine-orm symfony4

7
推荐指数
1
解决办法
601
查看次数

我的 Symfony 路由抛出 404?

我正在尝试重新开始开发,尝试使用 GoDaddy 在共享托管服务器上设置 Symfony。

我正在使用 Symfony 4(这里:http : //symfony.com/doc/current/page_creation.html)完成教程,但是URL.co.uk/lucky/number正在抛出 404。

我将完全相同的 URL 路由设置为 /(而不是幸运/数字)并且它工作正常。

routes.yaml和注释都会出现此问题。

<?php

namespace App\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;

class ArticleController

{
    /**
     * @Route("/")
     */
    public function homepage()
    {
        return new Response('OMG! My new first page already! ');
    }

    /**
     * @Route("/news")
     */
    public function news()
    {
        return new Response('This must be news?');
    }

}
Run Code Online (Sandbox Code Playgroud)

例如,以上对 / 工作正常,但使用 /news 时出现 404 错误。

这可能是一个问题.htaccess吗?我的根目录中没有。

事实证明,使用上面的代码,URL.co.uk/index.php/news工作得很好。显然我不想要这个,但我希望它有助于深入了解它......

php model-view-controller routing symfony symfony4

7
推荐指数
2
解决办法
1万
查看次数

Symfony 4 - 设置日期时间

所以我一直在关注这个数据库和Doctrine教程:https://symfony.com/doc/current/doctrine.html

唯一的区别是我添加了一个created_ts字段(在其他一些字段中,但它们工作正常,所以不需要进入它们).

我使用该make:entity命令生成我的类,并设置我created_ts生成的方法如下:

public function setCreatedTs(\DateTimeInterface $created_ts): self
{
    $this->created_ts = $created_ts;

    return $this;
}
Run Code Online (Sandbox Code Playgroud)

所以在我的/index页面中,我使用以下命令保存新实体:

$category->setCreatedTs(\DateTimeInterface::class, $date);
Run Code Online (Sandbox Code Playgroud)

我有一种有趣的感觉,这会出错,我是对的:

Type error: Argument 1 passed to App\Entity\Category::setCreatedTs() must implement interface DateTimeInterface, string given
Run Code Online (Sandbox Code Playgroud)

但我不知道如何实现DateTimeInterface内部功能..我尝试谷歌搜索但它显示了很多Symfony2帖子,一些我试图无济于事.

如何datetime->set方法中设置实体中的值?

(如果已经有答案,请链接.#symfonyScrub)

更新

# tried doing this:
$dateImmutable = \DateTime::createFromFormat('Y-m-d H:i:s', strtotime('now')); # also tried using \DateTimeImmutable

$category->setCategoryName('PHP');
$category->setCategoryBio('This is a category for PHP');
$category->setApproved(1);
$category->setGuruId(1);
$category->setCreatedTs($dateImmutable); # …
Run Code Online (Sandbox Code Playgroud)

datetime symfony symfony4

7
推荐指数
1
解决办法
1万
查看次数

Webpack Encore Jquery最大调用堆栈大小

我升级到Symfony 4.1并切换到Encore后面临一个问题.

Uncaught RangeError: Maximum call stack size exceeded
at _typeof (bundle.js?v=1.6565:3454)
at _typeof (bundle.js?v=1.6565:3454)
at _typeof (bundle.js?v=1.6565:3454)
at _typeof (bundle.js?v=1.6565:3454)
at _typeof (bundle.js?v=1.6565:3454)
at _typeof (bundle.js?v=1.6565:3454)
at _typeof (bundle.js?v=1.6565:3454)
at _typeof (bundle.js?v=1.6565:3454)
at _typeof (bundle.js?v=1.6565:3454)
at _typeof (bundle.js?v=1.6565:3454)
Run Code Online (Sandbox Code Playgroud)

我试图搜索问题的来源,并且只有在使用来自compose.js(和包含)的jquery include删除所有内容后,Error才会消失.但当然我需要jquery ......

一旦我从'jquery''或'Encore.autoProvidejQuery()'导入jQuery,就会发生错误

Webpack.config.js:

const Encore = require('@symfony/webpack-encore');

let publicPath = '/build/';
if (Encore.isProduction()) { // yarn run encore production
    publicPath = '/****_symfony/build/';
}

Encore
    .setOutputPath('public/build/')
    .setPublicPath(publicPath)
    .setManifestKeyPrefix('build/')
    .cleanupOutputBeforeBuild()
    .enableSourceMaps(!Encore.isProduction())
    .addEntry('bundle', './assets/compose.js')
    .enableStylusLoader()
    .enableLessLoader()
    .enableSassLoader()
;

const config = Encore.getWebpackConfig(); …
Run Code Online (Sandbox Code Playgroud)

javascript jquery webpack-encore symfony4

7
推荐指数
1
解决办法
395
查看次数

Symfony 4,Postgres - `参数"client_encoding"的值无效:"utf8mb4"`运行doctrine命令

问题

我有一个新的设置postgres 10.5symfony 4应用程序运行php 7.1.但是当我尝试运行迁移时.我一直收到以下Invalid value for parameter "client_encoding": "utf8mb4"错误. 在此输入图像描述

重现步骤

  1. .env文件上修改DSN以根据您的设置更正值.我是DATABASE_URL="pgsql://postgres:password@db:5432/a_db".
  2. 使用创建一个实体(Any会这样做) php bin/console make:entity
  3. 制作迁移文件 php bin/console make:migration

预期结果

我应该收到Success消息.

所以我的问题是

我遵循文档时在这里错过了什么?

php postgresql symfony symfony4

7
推荐指数
1
解决办法
4287
查看次数

如何将依赖于API-Platform的序列化组的属性公开给react-admin?

我在我的应用程序中动态更改序列化上下文admin:write在用户是管理员时应用该组.这样管理员的用户就可以更新此属性.

上下文构建器具有以下配置:

<?php

namespace App\Serializer;

use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

final class AdminContextBuilder implements SerializerContextBuilderInterface
{
    private $decorated;
    private $authorizationChecker;

    public function __construct(SerializerContextBuilderInterface $decorated, AuthorizationCheckerInterface $authorizationChecker)
    {
        $this->decorated = $decorated;
        $this->authorizationChecker = $authorizationChecker;
    }

    public function createFromRequest(Request $request, bool $normalization, ?array $extractedAttributes = null): array
    {
        $context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes);

        if (isset($context['groups']) && $this->authorizationChecker->isGranted('ROLE_ADMIN') && false === $normalization) {
            $context['groups'][] = 'admin:write';
        }
        if (isset($context['groups']) && $this->authorizationChecker->isGranted('ROLE_ADMIN') && true === $normalization) { …
Run Code Online (Sandbox Code Playgroud)

php symfony api-platform.com symfony4 react-admin

7
推荐指数
1
解决办法
405
查看次数

如何在 Symfony 4 表单的输入上添加占位符?

在我的 Symfony 4 表单中,我试图为我的输入获取一个占位符。我尝试了以下操作,但出现不允许的错误。任何想法我还能如何实现这一目标?

        ->add('firstname', TextType::class, ['label' => 'Vorname ', 'placeholder' => 'Your name',])
Run Code Online (Sandbox Code Playgroud)

正如文档中所建议的,我也尝试了下面的方法,在这里我没有收到错误,只是没有渲染。

        ->add('firstname', TextType::class, ['label' => 'Vorname '], ['attr' => ['placeholder' => 'Vorname ']])
Run Code Online (Sandbox Code Playgroud)

html symfony4

7
推荐指数
1
解决办法
7288
查看次数

如何在 SMTP 传输中处理 Symfony Mailer 组件凭据中的特殊字符?

语境

在 Symfony 4.3 中引入了一个新的电子邮件程序。

看这里:

对于 SMTP 传输,已确定 ENV 变量中的 DSN 具有以下格式:

MAILER_DSN=smtp://user:pass@smtp.example.com
Run Code Online (Sandbox Code Playgroud)

写DSN时如何处理用户名和密码中的特殊字符?

让我们假设凭据是:

username: alice@example.com
password: the:password:is:difficult
Run Code Online (Sandbox Code Playgroud)

像这样的设置很可能会失败:

MAILER_DSN=smtp://alice@example.com:the:password:is:difficult@smtp.example.com
Run Code Online (Sandbox Code Playgroud)

我应该如何编码/转义东西?什么是正确的 DSN?

smtp credentials special-characters mailer symfony4

7
推荐指数
1
解决办法
1770
查看次数