digbang/laravel-project

项目名称

安装: 149

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 3

Forks: 23,961

类型:项目

v9.3.0 2023-01-16 12:27 UTC

README

安装

  1. 克隆仓库
  2. 启动容器
  3. 访问PHP容器并

A. 运行 composer config -g github-oauth.github.com <token>(创建token请访问:https://github.com/settings/tokens/new 并设置repo权限)

B. 运行 composer install

C. 运行 ln -s /proxies proxies

D. 运行 composer build

Minio配置

  1. 将"127.0.0.1 s3"添加到hosts文件中
  2. 访问https://:9000/minio,
  3. 使用位于docker-compose.yml中的访问和秘密密钥登录。
  4. 创建一个具有某些名称的存储桶。给存储桶添加一个读写策略。
  5. 在您的env文件中配置存储桶名称
  6. 将文件系统驱动程序更改为minio

Sentry配置

  1. 配置您的.env变量
  2. 在.env文件中启用Sentry

系统配置

发布资产

  1. php artisan vendor:publish --provider "Digbang\\Backoffice\\BackofficeServiceProvider" --tag assets --force

编译资产(Vite)

Vite是一个现代前端构建工具,它提供了一个极快的开发环境,并将您的代码捆绑到生产中。

为了在开发中提供资产,您应使用npm run dev命令(该命令编译资产并通过localhost:3000提供服务器),而在生产构建中应使用npm run build命令。

如果您使用Vite,请确保您的布局使用@vite指令并正确指向资源:@vite(['resources/css/app.css', 'resources/js/app.js'])

有关Vite的更多信息,请参阅官方文档:https://laravel.net.cn/docs/9.x/vite

巡逻 - 跟踪有漏洞或过时的依赖项

巡逻是一个优雅的命令行工具,可确保您的PHP项目的依赖项处于受控状态。

  1. composer patrol

Pint - 代码风格修复器

Laravel Pint是一个针对简约主义者的PHP代码风格修复器。Pint建立在PHP-CS-Fixer之上,使确保您的代码风格保持干净和一致变得简单。

  1. composer cs

特殊目录

权限

// at the root of the project (only on linux)
sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache

代理(确保存在符号链接...)

// ... if not; inside of the PHP container run
ln -s /proxies /proxies

请求仓库

如果您需要一个仓库,您可能这样做

示例

public function users(): ?array
{
    if ($this->input(self::USER_IDS)) {
        return $this->repository(UserRepository::class)->find($this->input(self::USER_IDS));
    }

    return null;
}

如果您需要ReadRepository中不存在的仓库方法,您必须在您的请求中创建一个私有方法。

示例

private function roleRepository(): RoleRepository
{
    /** @var RoleRepository $repository */
    $repository = $this->repository(RoleRepository::class);

    return $repository;
}


public function roles(): ?array
{
    if ($this->input(self::ROLE_NAME)) {
        return $this->roleRepository()->findByName($this->input(self::ROLE_NAME));
    }

    return null;
}

HTTP状态码参考

以下列表包含API返回的HTTP状态码及其在本上下文中的含义

  • HTTP 200 Ok:请求已成功处理。
  • HTTP 201 Created:资源已创建。它与POST请求相关。
  • HTTP 204 No Content:请求已成功处理,但不需要返回实体主体。
  • HTTP 400 Bad Request:请求无法被API处理。您应检查发送的数据。
  • HTTP 401 Unauthorized:当请求执行到登录端点时,表示凭证与任何凭证都不匹配。当请求执行到另一个端点时,表示令牌已因TTL过期而无效。
  • HTTP 403 Forbidden:请求中提供的凭据没有必要的权限进行处理。
  • HTTP 404 Not Found:请求的端点在API中不存在。
  • HTTP 422:发送到API的数据包未通过验证过程。
  • HTTP 500:在处理过程中触发了未知错误。

请参考https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html以获取更多信息

Php Stan

现在您应该有一个phpstan.neon文件,允许您配置此包的基本设置。

https://github.com/nunomaduro/larastan https://phpstan.org/user-guide/getting-started

Php Insight

配置文件

现在您应该有一个config/insights.php文件,允许您配置此包的基本设置。

https://phpinsights.com/get-started.html

Laravel Responder

创建响应

return responder()
    ->success($user, UserProfileTransformer::class)
    ->meta([
        'meta' => $this->shouldRefreshToken(),
    ])
    ->respond();

创建转换器

php artisan make:transformer ProductTransformer --plain

处理异常

转换自定义异常

除了让包转换Laravel异常之外,您还可以使用render方法中的convert方法转换您自己的异常

$this->convert($exception, [
    InvalidValueException => PageNotFoundException,
]);

如果您想传递构造函数参数,可以可选地提供一个闭包来抛出新异常

$this->convert($exception, [
    MaintenanceModeException => function ($exception) {
        throw new ServerDownException($exception->retryAfter);
    },
]);

创建HTTP异常

异常类是存储错误信息的便利位置。该包提供了一个抽象异常类Flugg\Responder\Exceptions\Http\HttpException,它了解状态码、错误码和错误消息。继续使用我们上面的产品示例,我们可以创建自己的HttpException

<?php

namespace App\Exceptions;

use Flugg\Responder\Exceptions\Http\HttpException;

class SoldOutException extends HttpException
{
    /**
     * The HTTP status code.
     *
     * @var int
     */
    protected $status = 400;

    /**
     * The error code.
     *
     * @var string|null
     */
    protected $errorCode = 'sold_out_error';

    /**
     * The error message.
     *
     * @var string|null
     */
    protected $message = 'The requested product is sold out.';
}

您还可以添加一个返回额外错误数据的data方法

/**
 * Retrieve additional error data.
 *
 * @return array|null
 */
public function data()
{
    return [
        'shipments' => Shipment::all()
    ];
}

如果您让包处理异常,现在您可以在应用程序的任何地方抛出异常,它将自动渲染为错误响应。

throw new SoldOutException();

https://github.com/flugger/laravel-responder

JWT Auth

配置文件

现在您应该有一个config/jwt.php文件,允许您配置此包的基本设置。

####生成密钥

我包含了一个辅助命令来为您生成密钥

php artisan jwt:secret

这将更新您的.env文件,类似于JWT_SECRET=foobar

这是用于签名令牌的密钥。具体如何进行将取决于您选择的算法。

https://github.com/tymondesigns/jwt-auth

系统要求

  • php: 7.4.x

  • php ini配置

    • upload_max_filesize = 100M
    • post_max_size = 100M
    • 这些数字仅供参考。根据项目需求进行设置。
  • php扩展

    • bcmath
    • 核心
    • ctype
    • curl
    • date
    • dom
    • fileinfo
    • filter
    • ftp
    • gd
    • hash
    • iconv
    • imagick
    • intl
    • json
    • libxml
    • mbstring
    • mcrypt
    • mysqlnd
    • openssl
    • pcntl
    • pcre
    • PDO
    • pdo_pgsql
    • pdo_sqlite
    • Phar
    • posix
    • readline
    • redis
    • 反射
    • session
    • SimpleXML
    • soap
    • SPL
    • sqlite3
    • 标准
    • tidy
    • tokenizer
    • xdebug
    • xml
    • xmlreader
    • xmlwriter
    • ZendOPcache
    • zip
    • zlib
  • Composer PHP

  • apache: 2.4.x / nginx

  • postgres: 11.x / 12.x

  • postgres 扩展

    • Unaccent 扩展
  • redis

  • node

  • npm

  • yarn

  • SO 包

    • locales
    • locales-all

覆盖 php 配置

覆盖 php ini 默认配置

为了覆盖 .ini 配置,请使用项目根目录下的 ./docker/php/conf.d 目录中的 custom.ini 文件。修改文件后,您需要重新构建容器: docker-compose up -d --build php

建议更改 upload_max_filesizepost_max_size。请注意,这些值应该在所有环境中更改。

如果您需要,建议更改 memory_limit。在这种情况下,仅在开发环境中更改。

覆盖扩展配置

为了覆盖任何扩展设置,您应该能够在应用程序根目录相对的 ./docker/php/conf.d 文件夹中放置相应的 .ini 文件。

请记住,按照惯例,任何扩展配置文件都应该命名为 docker-php-ext-{extension name}.ini

例如,如果您想覆盖 opcache,您应该创建以下文件: ./docker/php/conf.d/docker-php-ext-opcache.ini 并填写所需内容。同样,对于 xdebug./docker/php/conf.d/docker-php-ext-xdebug.ini

然后,使用以下命令将文件从主机复制到容器中,在 ./docker/php/Dockerfile 内部: COPY conf.d /usr/local/etc/php/conf.d/

这将在下一次 docker-compose up --build 时生效

注意

If you want to test different configurations, you can mount the files in conf.d as volumes. That way, restarting apache or the container will use the new configurations.
To do so, you can add this (as an example) to the volumes configured on docker-compose.yml:

- .:/docker/php/conf.d/custom.ini:/usr/local/etc/php/conf.d/custom.ini:ro

After finishing with the tests and changes, please remove the volume configuration and rebuild the container with the new configs.