PHP Classes

How to Use a PHP Link to HTML Page Class to Generate Links or Form Buttons to Link Pages to Other Sites Using the Package Link Generator: Generate HTML for links and forms from templates

Recommend this page to a friend!
     
  Info   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2026-02-15 (7 days ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
linkgenerator 1.0.0Public Domain8HTML, Templates, PHP 8
Description 

Author

This package can generate HTML for links and forms from templates.

It provides a class that can generate HTML for links or forms that can lead users to specific pages.

The class has support to generate HTML for the links and forms using templates.

It can replace variable values in the HTML templates from parameter values.

Picture of Melanie Wehowski
  Performance   Level  
Name: Melanie Wehowski <contact>
Classes: 31 packages by
Country: Germany Germany
Innovation award
Innovation award
Nominee: 12x

Instructions

Overview

StartDir\LinkGenerator is a template-driven URL and form generator.

Code formatting and documentation was created with the help of ChatGPT!

Get the latest Version here.

It allows you to:

Register URL templates per software and module

Generate links dynamically from placeholders

Automatically generate matching HTML forms

Validate that all placeholders are replaced

Separate URL structure from runtime values

The class is especially useful when generating search or action URLs for platforms such as Mastodon or Friendica, where URL patterns are predictable but instances differ.

Internal Architecture

Templates are stored as a two-dimensional array:

[
    'software' => [
        'module' => 'https://{instance}/path?q={q}'
    ]
]

Placeholders use {name} syntax.

Reserved placeholder:

{instance} &#8594; always replaced automatically

All other placeholders must be supplied via parameters (for links) or are converted into form fields (for forms).

Constructor

public function __construct(array $defaultTemplates = [])

Allows optional default templates to be injected during instantiation.

Example

$generator = new \StartDir\LinkGenerator([
    'mastodon' => [
        'search' => 'https://{instance}/search?q={query}'
    ]
]);


addTemplate()
public function addTemplate(
    string $software,
    string $module,
    string $template
): void

Registers or overwrites a template.

Software names are normalized to lowercase.

Modules are case-sensitive.

Templates may contain placeholders.

Example

$generator->addTemplate(
    'friendica',
    'search',
    'https://{instance}/search?q={q}'
);

Example

$generator = new \StartDir\LinkGenerator();
$generator->addDefaults();

if ($generator->templateExists('mastodon', 'search')) {

    echo $generator->generateLink(
        'mastodon.social',
        'mastodon',
        'search',
        ['q' => 'open source'],
    );

    echo $generator->getFormHtml(
        'mastodon.social',
        'mastodon',
        'search',
        'GET',
        [
              //   'q' => ''
        ],
	[
		'target'=>'_blank',		 
	],
    );
}
  Files folder image Files (1)  
File Role Description
Accessible without login Plain text file LinkGenerator.php Class Class file

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 0%
Total:0
This week:0