定制 nubox/laminas-controller-injector 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

nubox/laminas-controller-injector

最新稳定版本:1.0.0

Composer 安装命令:

composer require nubox/laminas-controller-injector

包简介

Inject Route parameters into Controller methods

README 文档

README

Description

The core feature of this “plugin” is the ability to set parameters for controller methods. Primarily to pass the value of a variable of a route directly to the method at the dispatch event as an argument. The new AbstractInjectorController must be used for this usage.

In addition, with the new dispatcher you can also do without the “Action” postfix for these methods.

Requirements

  • PHP 8.1 or above
  • Laminas MVC 3.7.0 or above

Installation

Use composer to install the package:

composer require nubox/laminas-controller-injector

Activate Plugin in our Laminas Application

return [
    // Retrieve list of modules used in this application.
    'modules' => [
        ...,
        Laminas\Mvc\Injector\Module::class, // or 'Laminas\Mvc\Injector'
    ],
    ...
];

Usage

Suppose you have a controller that looks like this:

class MyController extends AbstractInjectorController
{ 
    public function myMethod(string $param) { 
        // ... 
    } 
}

You can map routes to this controller method using the format controller::method and pass parameters from the routes directly into the method:

return [
    'router' => [
        'routes' => [
            'my-route' => [
                'type' => Laminas\Router\Http\Segment::class,
                'options' => [
                    'route' => '/my-route/:param',
                    'defaults' => [
                        'controller' => MyController::class,
                        'action' => 'myMethod'
                    ]
                ]
            ]
        ]
    ],
];

In the example above, the :param placeholder in the route gets passed directly as the $param argument to myMethod().

AbstractInjectorController

The provided \Laminas\Mvc\Injector\Controller\AbstractInjectorController is required as Controller extension. It matches the given parameter values into the requested action. The same variable name is required.

As soon as the AbstractInjectorController is used, in addition to the route parameters, objects from the ServiceManager are also injected into the corresponding controller method - regardless of the route. This reduces the processes necessary to access a service from the ServiceManager.

The respective request can also be injected as a complete request object.

class MyController extends AbstractInjectorController
{
    public function serviceParameter(DemoService $demoService): Response
    {
        $response = new Response();
        $response->setContent($demoService->getValue());

        return $response;
    }
}

Available ArgumentResolver -> (configurable controller_argument_resolver)

Every argument of a controller method is analyzed and can be determined by the activated ArgumentResolver. The following ArgumentResolvers are activated by default. However, additional ArgumentResolvers can also be added.

Activated by default:

  • IntegerArgumentResolver - inject a int, parsed from Route parameters (int required)
  • StringArgumentResolver - inject a string , parsed from Route parameters (no typehint or string required)
  • RequestArgumentResolver - inject the responding Request object into the method (Request required)
  • ServiceArgumentResolver - inject an object from the container (ServiceManager) (explicit object Typehint required)
return [
    /**
     * each resolver need to implement ArgumentResolverInterface:class
     */
    'controller_argument_resolver' => [
        StringArgumentResolver::class,
        IntegerArgumentResolver::class,
        RequestArgumentResolver::class,
        ServiceArgumentResolver::class,
    ],
];

Normally you need a corresponding type hint to determine the correct ArgumentResolver. In the event that no suitable type can be found, you can use the MarkUp interface NonTypeArgumentResolverInterface for your own ArgumentResolver. this would then also be “queried” in such a case.

Suggestion

Use nubox/laminas-router-attributes for reduce configuration overhead with symfony route attributes.

class MyController extends AbstractInjectorController
{ 
    #[Route(path: 'calc-optional/{operand1}/{operand2?100}', name: 'calc-optional-route')]
    public function calculateWithRouteDefault(int $operand1, int $operand2 = 100): Response
    {
        $response = new Response();
        $response->setContent($operand1 * $operand2);

        return $response;
    }
    
    #[Route(path: 'default', name: 'default-route')]
    public function somedefaults(Request $request, string $default = 'defaults'): Response
    {
        $response = new Response();
        $response->setContent($request->getUri()->getPath() . ' - ' . $default);

        return $response;
    }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2023-12-29