PHPackages                             germania-kg/routenameurlcallable - 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. [Templating &amp; Views](/categories/templating)
4. /
5. germania-kg/routenameurlcallable

ActiveLibrary[Templating &amp; Views](/categories/templating)

germania-kg/routenameurlcallable
================================

Generates full URLs for Slim 4's named routes, using RouteContext und RouteParser. Suitable for Twig functions

2.3.0(3y ago)11.4k[2 PRs](https://github.com/GermaniaKG/RouteNameUrlCallable/pulls)MITPHPPHP ^7.4|^8.0

Since Nov 15Pushed 3y ago2 watchersCompare

[ Source](https://github.com/GermaniaKG/RouteNameUrlCallable)[ Packagist](https://packagist.org/packages/germania-kg/routenameurlcallable)[ RSS](/packages/germania-kg-routenameurlcallable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (5)Versions (22)Used By (0)

[![](https://camo.githubusercontent.com/cac3140c0c6e758f67a1ba689683ced67aa2d534e2187d6e03c5c721ffe3b976/68747470733a2f2f7374617469632e6765726d616e69612d6b672e636f6d2f6c6f676f732f67612d6c6f676f2d323031362d7765622e7376677a)](https://camo.githubusercontent.com/cac3140c0c6e758f67a1ba689683ced67aa2d534e2187d6e03c5c721ffe3b976/68747470733a2f2f7374617469632e6765726d616e69612d6b672e636f6d2f6c6f676f732f67612d6c6f676f2d323031362d7765622e7376677a)

---

Germania KG · RouteNameUrlCallable
==================================

[](#germania-kg--routenameurlcallable)

**Callable for generating full URLs using Slim 4's *RouteContext* and *RouteParser*. Works well as Twig function.**

[![Packagist](https://camo.githubusercontent.com/128cd11da0abc0e493d43eee777b5a97062e2adca03b2bcdf83a1fc2e6ce881e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6765726d616e69612d6b672f726f7574656e616d6575726c63616c6c61626c652e7376673f7374796c653d666c6174)](https://packagist.org/packages/germania-kg/routenameurlcallable)[![PHP version](https://camo.githubusercontent.com/e3a681a700f01101f79bbc85705e0ff04861497793bd3bace2734eb962c96f4f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6765726d616e69612d6b672f726f7574656e616d6575726c63616c6c61626c652e737667)](https://packagist.org/packages/germania-kg/routenameurlcallable)[![Tests](https://github.com/GermaniaKG/RouteNameUrlCallable/actions/workflows/tests.yml/badge.svg)](https://github.com/GermaniaKG/RouteNameUrlCallable/actions/workflows/tests.yml)

Installation with Composer
--------------------------

[](#installation-with-composer)

This package requires Slim Framework 4. For using Slim Framework 3, checkout the *v1* branch.

```
$ composer require germania-kg/routenameurlcallable "^2.0"
```

Alternatively, add this package directly to your *composer.json:*

```
"require": {
    "germania-kg/routenameurlcallable": "^2.0"
}
```

Introduction
------------

[](#introduction)

The controller callable within a Slim route sometimes needs a full URL, be it for a redirect response or for rendering links. Consider this route which creates a new *thing* via POST and redirects to its GET representation. The redirect requires a full URL.

Given a **named Slim route** like this…

```
$app->get("/hello/{name}", function($request, $response, $args) {
  $response->getBody()->write("Hello " . $args['name'] );
  return $response;
})->setName("Hello");
```

…**Slim framework** provides a solution using *RouteContext* and *RouteParser*:

```
$app->post("/users", function($request, $response, $args) {
  // Create new user and grab name
  $user = "john";
  $route = "Hello";

  // The Slim way
  $uri      = $request->getUri();
  $context  = \Slim\Routing\RouteContext::fromRequest($request);
  $parser   = $context->getRouteParser();
  $location = $parser->fullUrlFor($uri, $route, ['name' => $user]);
  # http://test.com/hello/john

  return $response->withHeader('Location', $location)->withStatus(302);
});
```

The **RouteNameUrlCallable** now hides the cumbersome stuff. It works basically as a shortcut:

```
$app->post("/users", function($request, $response, $args) {

  // Create new user and grab name
  $user = "john";
  $route = "Hello";

  $uri_factory = new RouteNameUrlCallable($request);
  $location = $uri_factory($route, ['name' => $user]);
  # http://test.com/hello/john

  return $response->withHeader('Location', $location)->withStatus(302);
});
```

Instantiation
-------------

[](#instantiation)

Pass a `Slim\Psr7\Request` instance to the constructor. This is the default variant as of release 2.

```
$rnc = new RouteNameUrlCallable($request);
```

As of release 2.2, it is also possible to inject a `Slim\Interfaces\RouteParserInterface`. This way will become the standard as of major release 3 (which at the time of writing has no time schedule).

```
use Slim\Routing\RouteContext;

$request = ...;
$route_parser = RouteContext::fromRequest($request)->getRouteParser();

$rnc = new RouteNameUrlCallable($route_parser);
```

**Recommendation:** As a replacement for injecting the request into the constructor, one is encouraged to use the static `fromRequest` method.

```
$rnc = RouteNameUrlCallable::fromRequest($request);
```

Usage
-----

[](#usage)

While Slim's **RouteParser's** *relativeUrlFor, relativeUrlFor, and fullUrlFor* methods return a string, the **RouteNameUrlCallable** returns a **Slim\\Http\\Uri** instance:

```
