小编yiv*_*ivi的帖子

使用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
查看次数

API平台-具有JWT身份验证的Swagger UI

我想在Swagger上添加“授权”按钮,如此处所述:https : //api-platform.com/docs/core/jwt#documenting-the-authentication-mechanism-with-swaggeropen-api

我安装了LexikJWTAuthenticationBundle,它与Curl一起正常工作。但是当我浏览到http:// localhost:8000 / api时,我只会看到{"code":401,"message":"JWT Token not found"}

我想念什么吗?

这是我的security.yaml

security:
    encoders:
        App\Entity\User:
            algorithm: bcrypt
    providers:
        db_provider:
            entity:
                class: App\Entity\User
                property: username
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        api_login:
            pattern: ^/api/login
            stateless: true
            anonymous: true
            form_login:
                check_path: /api/login_check
                success_handler: lexik_jwt_authentication.handler.authentication_success
                failure_handler: lexik_jwt_authentication.handler.authentication_failure
                require_previous_session: false
        api:
            pattern: ^/api
            stateless: true
            guard:
                authenticators:
                    - lexik_jwt_authentication.jwt_token_authenticator
        main:
            pattern: ^/
            anonymous: ~
            provider: db_provider
            form_login:
                login_path: app_security_login
                check_path: app_security_login
                csrf_token_generator: security.csrf.token_manager
            logout:
                path: /logout
                target: …
Run Code Online (Sandbox Code Playgroud)

symfony lexikjwtauthbundle api-platform.com

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

如何从Google_Service_PeopleService获取数据?

我目前正在使用Google_Client api并想要获取用户名,电话,电子邮件和用户地址.

我设置了这些范围:

'https://www.googleapis.com/auth/plus.login',
'https://www.googleapis.com/auth/user.birthday.read',
'https://www.googleapis.com/auth/user.addresses.read',
'https://www.googleapis.com/auth/user.emails.read',
'https://www.googleapis.com/auth/user.phonenumbers.read'
Run Code Online (Sandbox Code Playgroud)

当我点击google登录时,它会询问正确的权限,然后我会使用Google提供的代码获取访问令牌.

获取我请求的令牌people_service和配置文件数据后,如下所示:

$token = $this->client->fetchAccessTokenWithAuthCode($_GET['code']);
$people_service = new \Google_Service_PeopleService($this->client);
$profile = $people_service->people->get(
             'people/me', 
             array('personFields' => 'addresses,birthdays,emailAddresses,phoneNumbers')
            );
Run Code Online (Sandbox Code Playgroud)

它返回一个Google_Service_PeopleService_Person对象.

但是,当我尝试使用它时,就像getPhoneNumbers()它返回一个 Call to undefined method Google_Service_PeopleService_Person::getNames()错误.

有什么问题,我该怎么办?

php google-client google-client-login

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

为什么不能将信息写入/ tmp目录而不是/ var/www/html?

LAMP安装在我的本地电脑上,因为我知道该字符串xxxx可以/tmp/test用下面的PHP函数写入.

file_put_contents("/tmp/test","test1 test2") 
Run Code Online (Sandbox Code Playgroud)

cat ajax_get.php

<?php
    $str = "test1 test2";
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    $str = implode(" ",$_GET);
    file_put_contents("/tmp/test",$str);
    print($str);
?>
Run Code Online (Sandbox Code Playgroud)

为什么命令file_put_contents("/tmp/test",$str);ajax_get.php不能工作?

这是没有用的,以取代file_put_contents

$handle=fopen("/tmp/test","w");
fwrite($handle,$str);
fclose($handle);
Run Code Online (Sandbox Code Playgroud)

如果我更改下面的语句,可能是目录权限的问题 ajax_get.php

    file_put_contents("/tmp/test",$str);
Run Code Online (Sandbox Code Playgroud)

    file_put_contents("test",$str);
Run Code Online (Sandbox Code Playgroud)

并运行上一个进程,在其中ajax_get.php创建一个文件/var/www/html/test

cat /var/www/html/test
test1 test2
Run Code Online (Sandbox Code Playgroud)

显示/tmp目录的权限.

ls -al /tmp
total 76
drwxrwxrwt 16 root    root    12288 Dec 10 18:39 .
drwxr-xr-x 23 root    root     4096 Dec  1 08:03 ..
Run Code Online (Sandbox Code Playgroud)

.是当前目录/tmp,其权限是777(rwxrwxrwx),为什么不能通过/tmpPHP …

php file-put-contents

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

从pdf文件中读取注释

在此处输入图片说明

我有一个PDF文件,其中包含注释和对注释的建议,这些注释显示在鼠标移到带注释字词的上方时。

例如,考虑上面的图像,其中您要花费的单词是删除线(表示不正确的单词),并且在鼠标上方的上方显示了弹出窗口,其中出现了正确的单词。类似地,还有另一个插入符号也是如此。

我想提取两个单词的列表,这将显示文件中正确和错误的单词。

javascript php pdf

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

FOS UserBundle UserManager::__construct() 必须是 ObjectManager 的实例

全新安装 symfony 4.4,然后是 symfony flex 和一些常见的配方,然后我试图设置 fos userbundle,我遇到了这个问题,我无法解决。

Argument 3 passed to FOS\UserBundle\Doctrine\UserManager::__construct() must be an instance of Doctrine\Common\Persistence\ObjectManager, instance of Doctrine\ORM\EntityManager given, called in /srv/www/alexseif/var/cache/dev/ContainerYzHcxci/srcApp_KernelDevDebugContainer.php on line 1660
Run Code Online (Sandbox Code Playgroud)

symfony fosuserbundle symfony4

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

无法访问“最新”版本,没有注册的迁移

因此,我正在设置一个 Symfony 5 项目并运行以下命令来从实体注释生成数据库,如下所示:

php bin/console doctrine:schema:validate
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
Run Code Online (Sandbox Code Playgroud)

但它没有按预期工作,相反,我只是收到此错误:

**[ERROR]** The version "latest" couldn't be reached, there are no registered migrations.
Run Code Online (Sandbox Code Playgroud)

diff命令正确生成包含up()down()函数的迁移文件,但是当我随后运行migrate命令来生成数据库时,它会失败并出现上述错误。

我还注意到文件 /config/packages/doctrine_migrations.yml 最近已更改为:

doctrine_migrations:
    migrations_paths:
        'App\Migrations': '%kernel.project_dir%/src/Migrations'
Run Code Online (Sandbox Code Playgroud)

然而,学说似乎正在这条路径之外寻找以下路径中的迁移:

'%kernel.project_dir%/migrations'
Run Code Online (Sandbox Code Playgroud)

如何解决上述错误,以便migrate命令按预期工作并从生成的迁移文件生成数据库表?

php bin/console 调试:配置doctrine_migrations

Current configuration for extension with alias "doctrine_migrations"
=================================================================    ===

doctrine_migrations:
    migrations_paths:
        App\Migrations: /var/www/src/Migrations
    services: {  }
    factories: {  }
    storage:
        table_storage:
            table_name: null
            version_column_name: null
            version_column_length: null
            executed_at_column_name: null
            execution_time_column_name: null …
Run Code Online (Sandbox Code Playgroud)

php doctrine database-migration symfony

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

如何在容器中安装 Composer 依赖项?

我正在尝试做什么

我正在尝试对这个基于 CodeIgniter 4 构建的 php 应用程序进行 dockerize。该应用程序具有以下依赖项,这些依赖项通常通过 Composer 安装:

"require": {
    "php": "^7.3 || ^8.0",
    "myth/auth": "dev-develop",
    "codeigniter4/framework": "^4.1",
    "codeigniter4/translations" : "^4.0"
},
Run Code Online (Sandbox Code Playgroud)

项目结构组织如下:

docker
    database
    nginx
    php-fpm
    docker-compose.yml
src
    ... app files ...
tests
composer.json
Run Code Online (Sandbox Code Playgroud)

在里面docker-compose.yml我有一个名为的服务php-fpm,它将所有应用程序文件复制到公共/var/www/html目录nginx

php-fpm:
  container_name: boilerplate_app
  build:
    context: ./php-fpm
  volumes:
    - ../src:/var/www/html
Run Code Online (Sandbox Code Playgroud)

我需要做的是使用 Composer 安装应用程序依赖项。

我尝试过的

最初,我想在Dockerfile可用php-fpm文件夹中执行此操作:

FROM php:8.0.2-fpm-alpine

RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-install mysqli    

# Install Composer
COPY --from=composer /usr/bin/composer …
Run Code Online (Sandbox Code Playgroud)

php composer-php docker docker-compose

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

使用 PHP 8 属性时如何覆盖子类中 Doctrine 的字段关联映射?

如何在父类中定义 Doctrine 属性并覆盖扩展父类的类中的关联?当使用注释时,这是通过使用AssociationOverride实现的,但是,我认为它们在使用 PHP 8 属性时不可用

为什么我想:

我有一个类,AbstractTenantEntity其目的是将数据访问限制为Tenant拥有数据的给定(即帐户、所有者等),并且扩展该类的任何实体将tenant_id在创建时插入到数据库中,并且所有其他请求将添加tenant_id到 WHERE 子句。 Tenant通常没有扩展的各种实体的集合AbstractTenantEntity,但有一些有。当使用注释时,我通过将 Doctrine 的AssociationOverride注释应用到扩展类来处理它,这些扩展类应该在 中具有集合Tenant,但我不知道在使用 PHP 8 属性时如何实现这一点?


下面描述的我的尝试是不成功的,因为我错误地认为如果进行适当的修改,注释类将神奇地与属性一起工作,但现在我看到其他代码必须能够基于属性应用适当的逻辑。因此,我放弃了这种方法,只是将属性保护起来并在具体类中复制它们。

我的尝试:

租户实体

use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;

#[Entity()]
class Tenant
{
    #[Id, Column(type: "integer")]
    #[GeneratedValue]
    private ?int $id = null;

    #[OneToMany(targetEntity: Asset::class, mappedBy: 'tenant')]
    private array|Collection|ArrayCollection $assets;

    // Other properties and typical getters and …
Run Code Online (Sandbox Code Playgroud)

php attributes doctrine symfony doctrine-orm

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

为什么在 PHP 中使用“use”关键字来导入核心标识符?

有时在一些库中,比如thephpleague/csv,我看到use关键字导入核心标识符(例如函数、类或常量),但我根本不明白为什么要导入它们。

请参见以下示例:

use Generator;
use SplFileObject;
use function filter_var;
use function get_class;
use function mb_strlen;
use function rawurlencode;
use function sprintf;
use function str_replace;
use function str_split;
use function strcspn;
use function strlen;
use const FILTER_FLAG_STRIP_HIGH;
use const FILTER_FLAG_STRIP_LOW;
use const FILTER_SANITIZE_STRING;
Run Code Online (Sandbox Code Playgroud)

或者更多来自这里

该文件中导入的这些类、函数和常量属于 PHP 核心,因此我们实际上不需要导入它们。

为什么这个库和其他库导入这些?

php oop namespaces

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