承接 waldemarnt/user-agent-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

waldemarnt/user-agent-bundle

最新稳定版本:v1.0.6

Composer 安装命令:

composer require waldemarnt/user-agent-bundle

包简介

bundle to handle header user-agent versions

README 文档

README

Suppose that we have an API or a external connection that needs validation by user-agent, by version, this bundle will fall like a glove in your pretty hands.

Usage

The list is separated by kind.

Installation

Add it as dependency in your composer.json

    "require": {
        "waldemarnt/user-agent-bundle": "2.*"
    }

Update your AppKernel.php and add this line

    new Wneto\UserAgentBundle\WnetoUserAgentBundle(),

Usage

First of all you need understand how this stuff works. For example, a browser request send headers like this:

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.99 Safari/537.36

This example was did using POSTMAN, well, lets suppose that i need receive only requests from Mozilla that have version higher than 5.0. What i need to do that? Kepp calm, lets do it! First of all we need the configuration inside the config.yml file

# you can found a sample inside Wneto/UserAgentBundle/Resources/samples/config/user_agent.yml
wneto_user_agent:
    validation: true
    type: whitelist # blacklist or whitelist. With whitelist all request will be blocked and accept requests only from the setted patterns
    patterns:
        - { pattern: "Mozilla", version: "5.0", operator: ">" }

Lets understand, pattern is the name of the agent, the name is before the / bar in the agent, allowed is a boolean and the version will be the version that you need set the rule, the operator is used to applicate the rule for example higher than > higher of equals than >= the same in inverse < , <=

Ok now in our application we just need add a verification like this one: First inject the service @user_agent.validator.user_agent in your class.

    public function isDeviceAllowed($request){
        if($this->userAgentValidator->isEnabled()) {
            return $this->userAgentValidator->isAllowed($request->headers->get('user-agent'));
        }

        return true;
    }

Ok, the magic will happen and we will be happy forever.

统计信息

  • 总下载量: 183
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-10-01