PHP code example of nubox / laminas-controller-injector

1. Go to this page and download the library: Download nubox/laminas-controller-injector library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nubox / laminas-controller-injector example snippets


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

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

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

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

        return $response;
    }
}

return [
    /**
     * each resolver need to implement ArgumentResolverInterface:class
     */
    'controller_argument_resolver' => [
        StringArgumentResolver::class,
        IntegerArgumentResolver::class,
        RequestArgumentResolver::class,
        ServiceArgumentResolver::class,
    ],
];

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;
    }
}