Download the PHP package inanepain/routing without Composer

On this page you can find all versions of the php package inanepain/routing. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package routing

= inanepain/routing :author: Philip Michael Raab :email: [email protected] :keywords: routing, router, route, attribute :description: HTTP Routing using attributes. :revnumber: 1.4.0 :revdate: 2025-04-09 :copyright: Unlicense :experimental: :hide-uri-scheme: :icons: font :source-highlighter: highlight.js :toc: auto :sectanchors: :toclevels: 3

version: $Id$ ($Date$)

NOTE: examples updated to use RouteMatch.

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

:leveloffset: +1

= Install :author: Philip Michael Raab :email: [email protected] :keywords: routing, router, route, attribute :description: HTTP Routing using attributes. :revnumber: 1.4.0 :revdate: 2025-04-09 :copyright: Unlicense :experimental: :hide-uri-scheme: :icons: font :source-highlighter: highlight.js :toc: auto :sectanchors:

Installtion is as easy as asking composer to add the package as a requirement.

.require via composer composer require inanepain/routing

:leveloffset!: :leveloffset: +1

= Overview of Attributes :author: Philip Michael Raab :email: [email protected] :keywords: routing, router, route, attribute :description: HTTP Routing using attributes. :revnumber: 1.4.0 :revdate: 2025-04-09 :copyright: Unlicense :experimental: :hide-uri-scheme: :icons: font :source-highlighter: highlight.js :toc: auto :sectanchors:

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.

:leveloffset!: :leveloffset: +1

= Usage :author: Philip Michael Raab :email: [email protected] :keywords: routing, router, route, attribute :description: HTTP Routing using attributes. :revnumber: 1.4.0 :revdate: 2025-04-09 :copyright: Unlicense :experimental: :hide-uri-scheme: :icons: font :source-highlighter: highlight.js :toc: auto :sectanchors:

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.

== Examples :sectnums:

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

:leveloffset: +2

= Example: Simple :author: Philip Michael Raab :email: [email protected] :keywords: routing, router, route, attribute :description: HTTP Routing using attributes. :revnumber: 1.4.0 :revdate: 2025-04-09 :copyright: Unlicense :experimental: :hide-uri-scheme: :icons: font :source-highlighter: highlight.js :toc: auto :sectanchors:

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 [source,php]

class MainController { ...

#[Route(path: '/', name: 'home')]
public function home(): void {
    ...
    echo <<<HTML

... HTML; }

...

#[Route(path: '/product/{product}', name: 'product', )]
public function productTask(array $params): void {
    $sql = "...where product_id = '{$params['product']}'";
    ...
    echo <<<HTML

... HTML; }

...

}

Our "application" sits in index.php which will pass through existing files and try route everything else. + We simple add our MainController to the Router and then check for a match.

.index.php [source,php]

use App\Controller\MainController; use Inane\Routing\Router;

require_once 'vendor/autoload.php';

$file = 'public' . $_SERVER['REQUEST_URI'];

// Server existing files in web dir if (file_exists($file) && !is_dir($file)) return false;

$router = new Router(); // Add the controllers to the Router $router->addRoutes([MainController::class]);

if ($match = $router->match()) { // Check for a Route Match // create the controller $controller = new $match->class(); // and call the method with paramaters. $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.

:leveloffset: 1 :leveloffset: +2

= Example: Complete :author: Philip Michael Raab :email: [email protected] :keywords: routing, router, route, attribute :description: HTTP Routing using attributes. :revnumber: 1.4.0 :revdate: 2025-04-09 :copyright: Unlicense :experimental: :hide-uri-scheme: :icons: font :source-highlighter: highlight.js :toc: auto :sectanchors:

Now let's try a slightly more complex example.

== The various pieces

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

.IndexController.php [source,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<\d+>}', 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) [source,phtml]

... <?=$item['name_long']?> ...

That should give us this.

.website (rendered view) [source,html]

Mega Maid (Household Robot Helper)

Great.

== Putting it all together

Chuck that all into an app, I'm only showing the parts relavent to the routing.

.Application.php [source,php]

class Application { ...

protected function initialise(): void {
    ...
    $this->router = new Router([
        IndexController::class,
        ...
        WhoopsController::class,
        ...
    ]);
    ...
}

...

public function run(): void {
    ...
    if ($match = $this->router->match()) {
        $controller = new $match->class($match['params']);
        $data = $controller->{$match['method']}();
        ...
        // since 1.4.0: using the RouteMatch we can now easily get the template
        $body = $this->renderer->render($match->template, $data);
        ...
    }
    ...
}

...

}

... and you're of to the races.

:leveloffset: 1

:sectnums!:

:leveloffset!: :leveloffset: +1

= Appendix: .htaccess :author: Philip Michael Raab :email: [email protected] :keywords: routing, router, route, attribute :description: HTTP Routing using attributes. :revnumber: 1.4.0 :revdate: 2025-04-09 :copyright: Unlicense :experimental: :hide-uri-scheme: :icons: font :source-highlighter: highlight.js :toc: auto :sectanchors:

You will also need to do some magic in your .htaccess file so that index.php handles all requests.

[source,appache]

RewriteEngine On

The following rule tells Apache that if the requested filename exists, simply serve it.

RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [L]

The following rewrites all other queries to index.php. The

condition ensures that if you are using Apache aliases to do

mass virtual hosting or installed the project in a subdirectory,

the base path will be prepended to allow proper resolution of

the index.php file; it will work in non-aliased environments

as well, providing a safe, one-size fits all solution.

RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.)::\2$ RewriteRule ^(.) - [E=BASE:%1] RewriteRule ^(.*)$ %{ENV:BASE}/index.php [L]

# Deprecated apache 2.2 syntax: # Order Allow,Deny # Allow from all # Apache > 2.4 requires: Require all granted

:leveloffset!:


All versions of routing with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
inanepain/http Version >=0.1.3 || dev-master || dev-develop
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package inanepain/routing contains the following files

Loading the files please wait ....