PHPackages                             inanepain/routing - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. inanepain/routing

ActiveLibrary[HTTP &amp; Networking](/categories/http)

inanepain/routing
=================

HTTP Routing using attributes.

1.4.0(1y ago)1101UNLICENSEPHPPHP &gt;=8.1

Since Jul 26Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/inanepain/routing)[ Packagist](https://packagist.org/packages/inanepain/routing)[ Docs](http://vaieen.local:3000/inane/routing)[ RSS](/packages/inanepain-routing/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelogDependencies (1)Versions (7)Used By (0)

inanepain/routing [![icon](./icon.png "inanepain/routing")](./icon.png)
=======================================================================

[](#inanepainrouting-)

Table of Contents

- [![icon](./icon.png "inanepain/routing") inanepain/routing](#inanepainrouting)
- [1. Install](#install)
- [2. Overview of Attributes](#overview-of-attributes)
- [3. Usage](#usage)
    - [3.1. Examples](#examples)
- [Appendix: .htaccess](#appendix-htaccess)

[![icon](./icon.png "inanepain/routing")](./icon.png) inanepain/routing
-----------------------------------------------------------------------

[](#-inanepainrouting)

Note

examples updated to use RouteMatch.

HTTP Routing using attributes.

Add routing to your application by simply using attributes on your methods to configure the route.

1. Install
----------

[](#1-install)

composer

```
composer require inanepain/routing
```

2. Overview of Attributes
-------------------------

[](#2-overview-of-attributes)

What is an **Attribute**? It’s a class just like any other class only with the `Attribute` **Attribute**. So why are you treating it more like an `enum` or `Map` that can only hold a few values describing something? You don’t do it with the classes you uses your custom attributes on! But I don’t blame you, it all comes down to some pour choices in wording used by the documentation.

So how should I be think of **Attributes**? As classes naturally. Classes to object that get things done to be more exact. That `#[Route(name: 'home', path: '/')]` like might make more sense when you start looking at it like this: `$route = new Route('/', 'home');`. Here a fun experiment to try; remove the `Attribute` from `Route` then have the `Router` take an array of `Route` parameters as argument. Easy, wasn’t it and you understand Attributes and with practice you spot many more classes you can use as such.

Hope that gets you thinking about **Attributes** in a new, more realistic manor that leads to you adding that `#[Attribute]` line to a good many more classes.

3. Usage
--------

[](#3-usage)

Quick overview showing the bits relating to the `Route` `Attribute` in two examples. Neither are complete, though the simple example would run with minimum fuss. Check the Appendix for the `.htaccess` file you will need to use with the `index.php` file.

### 3.1. Examples

[](#31-examples)

An example is worth a thousand words, well here come two examples.

#### 3.1.1. Example: Simple

[](#311-example-simple)

Super simple example using **php** built in web server.
We create a class, let’s call it **MainController.php**, and add `Route` **attributes** to the methods we want routes to. The **path** is matched against the **url** with **regex**.

MainController.php

```
class MainController {
    ...

    #[Route(path: '/', name: 'home')]
    public function home(): void {
        ...
        echo addRoutes([MainController::class]);

if ($match = $router->match()) { // Check for a Route Match
    // create the controller
    $controller = new $match->class();
    // and call the method with parameters.
    $controller->{$match->method}($match->params);
} else { // Otherwise do what ever else, we'll through an error.
    throw new Exception('Request Error: Unmatched `file` or `route`!');
}
```

Now let’s file up php’s built in server:

php built-in server

`php -S localhost:8080 -t public index.php`

All thay’s left is to visit the url in your favorit browser.
And that’s a real basic emample of how it’s and it doesn’t really get much more complex.

#### 3.1.2. Example: Complete

[](#312-example-complete)

Now let’s try a slightly more complex example.

##### The various pieces

[](#the-various-pieces)

Again we setup our routes by using attributes on the controller methods.

IndexController.php

```
class IndexController extends AbstractController {
    ...

    #[Route(path: '/', name: 'home')]
    public function indexTask(): array {
        ...
    }

    ...

    #[Route(path: '/product/{product}', name: 'product', )]
    public function productTask(): array {
        ...
    }

    ...

    #[Route(path: '/product/{product}/review/{id}', name: 'product-review')]
    public function reviewTask(): array {
        ...
    }

    ...
}
```

But now we’re adding a view template to the mix. Not that this does much but it’s just for show. So here we render an anchor.

index.phtml (view template)

```
...
