Download the PHP package struktal/struktal-router without Composer

On this page you can find all versions of the php package struktal/struktal-router. 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 struktal-router

struktal-router

This is a PHP library that enables route handling in web applications.

Installation

To install this library, include it in your project using Composer:

Usage

Before you can use this library, you need to customize a few parameters. You can do this in the startup of your application:

Then, you can use the library's features in your code.

Defining routes

To define routes, you can use the Router class to register your routes.

A very simple example would be:

This registers a route that responds to GET requests at the root URL (/) and serves the index.php file, which is expected to handle the request. The index.php file must be located in the directory specified by setPagesDirectory.

You can also define routes with parameters:

This registers a route that responds to GET requests at /user/{i:userId}, where {i:userId} is a placeholder for an integer parameter named userId. The request will be handled by the users/details.php file, so the details.php file is now located in the subdirectory users/ within the directory specified by setPagesDirectory.

A route can have multiple placeholders, all of them are denoted by curly braces with the data type and name of the placeholder inside. The variable is then available in the $_GET superglobal array, so you can access it like this:

The available data types for placeholders are:

Finally, it is also possible to define routes that respond to multiple HTTP methods:

In this case, the route will respond to both GET and POST requests at the /user URL, and the handling user.php script could serve an HTML form to create or update a user upon a GET request, and process the form submission upon a POST request. However, this use-case is not recommended, as it is better to separate the handling of different HTTP methods into different routes for clarity and maintainability.

Defining error routes

After defining your routes, you have to define error routes. When stumbling upon a routing error, the users have to be redirected accordingly. You can do this as follows:

This will redirect the users to the existing routes named 400 and 404 to handle the respective errors.

Starting the router

When you have followed the above steps, you can simply start the router in your application:

More features

Generate routes

You can generate URLs for your defined routes using the generate method of the Router class:

This searches for the route named user_details and replaces the {i:userId} placeholder with the value 42, resulting in a URL like /user/42. You can pass an additional parameter withHostUrl as boolean to include the host URL in the generated output, resulting in a full URL like https://yourdomain.com/user/42.

Get paths for static files

You can also get the paths for static files using the getStaticFilePath method:

License

This software is licensed under the MIT license. See the LICENSE file for more information.


All versions of struktal-router with dependencies

PHP Build Version
Package Version
Requires php Version >=8.2.0
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 struktal/struktal-router contains the following files

Loading the files please wait ....