Coder Social home page Coder Social logo

laminas-router-attributes's Introduction

Using Symfony Route Attributes in Laminas Action Controller

This guide will walk you through on how to use Symfony Route Attributes in Laminas Action Controller in PHP 8.1.

Requirements

  • PHP 8.1 or higher

Installation

Install the required packages via composer.

composer require nubox/laminas-router-attributes

Activate Plugin in our Laminas Application

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

Usage

You can start by creating a new action controller which we will annotate with Symfony's Route attributes instead of the laminas configuration.

<?php 

namespace Module\Controller; 

use Laminas\Mvc\Controller\AbstractActionController;
use Symfony\Component\Routing\Attribute\Route;

class MyController extends AbstractActionController 
{ 
    #[Route('/my-path', name: 'my_route_name')] 
    public function myAction() { 
        //Your action code goes here...
    }
}

In the example above, #[Route('/my-path', name: 'my_route_name')] defines the path for the action and names the route.

That's all! You have successfully configured Symfony Route Attributes in your Laminas Action Controller.

Supported behavior from Symfony routes

  • configuration via Attributes
    • path -> domain route for methods or classes
    • name -> set up a name for the route
    • condition -> for more complex conditions about your route (require symfony/expression-language)
    • methods -> restrict the route to an explicit method
    • requirments -> Parameters Validation via RegExp
    • inline-defaults -> Inline default-settings (in combination with conditions, also)
    • defaults -> add default settings

Troubleshooting

If you run into any issue while trying to use Symfony Route attributes in your Laminas Action Controller, please refer to the respective Symfony and Laminas documentation.

Should you encounter issues that are not documented, kindly log them in our issues tracker.

Happy Coding!

laminas-router-attributes's People

Contributors

nusphere avatar

Watchers

 avatar Vasil Dakov avatar

laminas-router-attributes's Issues

Wildcard Route for Wildcard Actions

Feature Request

In Laminas it is possible to set the action route variable, which acts as a "catch-all" route for the method in the controller.

Q A
New Feature yes

Summary

The following controller is given:

<?php 

namespace Module\Controller; 

use Laminas\Mvc\Controller\AbstractActionController;
use Symfony\Component\Routing\Attribute\Route;

#[Route('/api/{action}', name: 'my_wildcard_route')] 
class MyController extends AbstractActionController 
{
    public function myAction() { 
        //Your action code goes here...
    }

    public function userListAction() { 
        //Your action code goes here...
    }
}
  • a route call /api/my should call MyController::myAction
  • following route calls should call MyController::myAction:
    • /api/userList
    • /api/userlist
    • /api/user-list
    • /api/user_list

Implement UrlGeneratorInterface as Service

Feature Request

Provide a service for generating a route by its route-name

Q A
New Feature yes

Summary

// src/Service/SomeService.php
namespace App\Service;

use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class SomeService
{
    public function __construct(
        private UrlGeneratorInterface $router,
    ) {
    }

    public function someMethod(): void
    {
        // ...

        // generate a URL with no route arguments
        $signUpPage = $this->router->generate('sign_up');

        // generate a URL with route arguments
        $userProfilePage = $this->router->generate('user_profile', [
            'username' => $user->getUserIdentifier(),
        ]);

        // generated URLs are "absolute paths" by default. Pass a third optional
        // argument to generate different URLs (e.g. an "absolute URL")
        $signUpPage = $this->router->generate('sign_up', [], UrlGeneratorInterface::ABSOLUTE_URL);

        // when a route is localized, Symfony uses by default the current request locale
        // pass a different '_locale' value if you want to set the locale explicitly
        $signUpPageInDutch = $this->router->generate('sign_up', ['_locale' => 'nl']);
    }
}

https://symfony.com/doc/current/routing.html#generating-urls-in-services

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.