Download the PHP package crastlin/laravel-annotation_v2 without Composer

On this page you can find all versions of the php package crastlin/laravel-annotation_v2. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-annotation_v2

laravel-annotation_v2 使用指南

从PHP8开始已经对注解(Attribute) 原生支持了,这有利于我们创建更快更好用的注解工具利器,为我们的编码工作带来更高的效率。之前开发的 PHP7.x + Laravel5.8.x 注解插件 在leanKu上受到不少朋友的关注,但大部分朋友已经全面切到PHP8+,希望能发布PHP8系列注解插件,虽然现在很多PHPer都转战Golang了,但PHP在我心中依旧占一席之地!所以我依然希望能为PHP开源尽点微薄之力,希望它越来越好,重回巅峰时刻。

1、环境和安装

1.1. 环境要求

1.2. 安装依赖包

`

Tips: 也可以在composer.json的 require内定义:"crastlin/laravel-annotation_v2": "^v1.1.5-alpha"

2、初始化配置文件

`

3、路由模块

3.1 路由定义

Tips: 需要在类上配置 Controller 注解,否则将被排除扫描

``

``

``

或者使用PostMapping

``

``

更多的路由注解,请查看Router接口的实现类,包括常用的 PostMapping / GetMapping / AnyMapping

3.2 路由分组

``

``

-- 对应的路由地址:/api/index/api/article

``

``

中间件还可使用 Middleware 注解绑定,中间件名称可以直接指定中间件文件地址,多个中间件可以定义成数组,例如 ``

``

Tips: Group 支持类注解和方法注解,可以配合 Domain / Middleware / Domain 注解使用
可以在 config/annotation.phproute 配置项中,配置 root_group 项,可实现模块化分组

``

3.3 资源路由

资源路由(Restful Api)是常用的接口实现方式,其以固定的请求方式和路由参数组合标准,可快速注册路由,可以使用注解:ResourceMappingApiResourceMapping 生成一组资源路由,实现接口:Crastlin\LaravelAnnotation\Extra\ResourceInterface,快速生成对应的方法

``

3.4 路由生成

`

Tips: 建议在生产环境配置hook,每次发版完成后,自动更新路由

4、菜单权限

在开发后台时,经常会需要使用到功能菜单和角色权限分配的功能,使用注解的好处在于,开发时定义好菜单树和权限节点信息,无需在数据库繁琐添加,只需要使用生成命令,快速将注解的菜单树和权限节点保存到数据库,方便环境切换和移植,为开发者整理菜单节约宝贵的时间。

4.1 配置第一个节点

``

`

`

生成对应的节点如下:

Tips: 每个控制器可配置成一个根节点,控制器内的所有方法默认都是该根节点的子节点。

4.2 配置父节点

``

生成对应的节点如下:

4.3 参数说明

Tips: 执行生成节点后,修改方法名将会产生无用的节点数据,无法删除,建议方法确定好后,再执行命令生成,否则只能清空 node 表中的数据,重新生成。

4.4 多态控制器应用

``

5、拦截器

5.1 请求并发锁

经常会遇到这样的场景,当同一个请求(公共级 / 用户级 / 项目级等)需要限制并发,可以使用SyncLock,注解控制器方法,达到限制并发的效果,SyncLock 采用的是 redis 的 set 实现方式,redis配置默认使用的Laravel env配置项,可自行在生成的 config/annotation.php 中配置。

5.1.1 使用 SyncLock 注解

``

  • 以上的 updateNews 方法,当并发相同 id 请求时,则会响应正在更新中的提示

5.1.2 可用参数说明

如果需要按登录身份进行并发限制,则可以使用 SyncLockByToken suffix参数可通过参数或配置文件 config/annotation.php 中的interceptor -> lock -> token 配置,默认配置为: {header.token}

5.2 验证器

5.2.1. 使用Validation注解快速开启一个验证器

``

5.2.2. 创建多条件验证

``

5.2.3. 创建验证器类

创建验证类需要继承基类Crastlin\LaravelAnnotation\Extra\Validate,该验证基类已实现了很多常用的方法,以及增加了callback验证器,可实现自定义方法实现验证

  • app目录下创建验证器目录Validate,并创建MessageCheck

Validate验证基类集成了大部分错误提示消息模板,可以省去每次定义

  • 在控制器中定义Validation注解,并设置参数:class\App\Validate\MessageCheck::class ``
  • 以上注解等同于

5.2.4 使用独立验证器注解

5.3. 参数验证

6、依赖注入

6.1. 实现依赖注入(包括:属性注入、构造方法注入、setter方法注入、参数注入)有以下三种方式:

  1. 控制器类需要继承Crastlin\LaravelAnnotation\Extra\BaseController
  2. 自定义类需要使用InvokeTrait,并使用类调用方式实现中间方法转发调用,(即魔术方法__invoke的使用)
  3. 接口方式注入的实现需要定义Service接口层和Impl类实现层,通过匿名代理类,实现懒加载的效果,性能更强(推荐使用)

Tips: InvokeTrait中已经定义好常用的:请求数据data,响应码resCode,错误信息errText、 返回数据result,以及对应的get方法,可以使用统一方法返回值(bool)和类响应标准,有利于开发效率和标准化,减少BUG,强烈推荐使用!

6.1.1. 控制器类依赖注入,以下示例实现了环境配置注入配置注入请求参数注入

6.1.2. 自定义注入

6.1.3. 参数注入

6.1.4. 自定义类依赖注入

以上示例中,updateAvatar方法参数$avatar注入规则:自定义绑定数据 > 请求参数(form 或 json)avatar字段

6.2. 接口依赖注入

使用接口依赖注入前先要定义好Service层和Impl实现层

  • 先定义好一个Service层接口 app/Service/UserService.php

Tips 使用接口类型注入只能使用 Autowired注解,且实现类必须标记Service注解,否则会被排除。使用Qualifier指定实现类可以是类名或类命名空间地址。如果非工厂模式,Service层可以不定义任何方法,只需在头部增加 @mixin App\Service\Impl\User 则Idea会自动映射方法提示

7、代码贡献

Crastlin博客主页 [email protected]

8、使用必读


All versions of laravel-annotation_v2 with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
ext-json Version *
ext-redis Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package crastlin/laravel-annotation_v2 contains the following files

Loading the files please wait ....