Oihana PHP System

withPlaceholder.php

Table of Contents

Functions

withPlaceholder()  : string
Builds a Slim-framework-compatible route by appending a placeholder.

Functions

withPlaceholder()

Builds a Slim-framework-compatible route by appending a placeholder.

withPlaceholder(string $route[, string|null $placeholder = null ][, bool $optional = false ][, bool $leadingSlash = true ]) : string

This helper function assembles a base route and a placeholder segment, automatically handling trailing slashes, optional segments, and empty placeholders. It is designed to simplify the creation of dynamic route patterns.

Parameters
$route : string

The base route path (e.g., '/users').

$placeholder : string|null = null

Placeholder name, optionally with regex (e.g., 'id', 'id:[0-9]+', 'params:.*')

$optional : bool = false

If true, wrap the placeholder in square brackets

$leadingSlash : bool = true

If true, a leading slash / will be prepended to the placeholder (Default true).

Tags
example
// Placeholder requis simple
withPlaceholder('/users', 'id');
// -> '/users/{id}'

// Placeholder requis avec une contrainte regex
withPlaceholder('/articles', 'slug:[a-z0-9-]+');
// -> '/articles/{slug:[a-z0-9-]+}'

// Placeholder optionnel
withPlaceholder('/search', 'query', true);
// -> '/search[/{query}]'

// Placeholder "catch-all" optionnel pour capturer plusieurs segments
withPlaceholder('/files', 'path:.*', true);
// -> '/files[/{path:.*}]'

// Gère correctement le slash final sur la route de base
withPlaceholder('/products/', 'sku');
// -> '/products/{sku}'

// Cas d'un placeholder vide ou null
withPlaceholder('/home', null);
// -> '/home'

// Sans ajout de slash initial (cas d'usage avancé)
withPlaceholder('/user-', 'id', false, false);
// -> '/user-{id}'
Return values
string

Slim route with placeholder


        
On this page

Search results