Download the PHP package julienlinard/php-router without Composer

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

PHP Router

🇬🇧 Read in English

💝 Support the project

If this bundle is useful to you, consider becoming a sponsor to support the development and maintenance of this open source project.


A modern and complete PHP router for managing your application routes with support for dynamic routes, middlewares, and all essential features.

📋 Table of Contents

🚀 Installation

Use Composer to install the package:

Requirements: PHP 8.0 or higher

⚡ Quick Start

🛣️ Route Definition

Routes are defined in your controllers using the Route attribute (PHP 8).

Simple Route

Routes with Multiple HTTP Methods

Route Registration

Route Groups

Route groups allow you to organize your routes with a common prefix and shared middlewares.

Complete Example:

🔄 Dynamic Routes

The router supports dynamic routes with parameters automatically extracted from the URL.

Route with One Parameter

Example URL: /user/123$userId = '123'

Route with Multiple Parameters

Example URL: /user/123/post/my-article$userId = '123', $slug = 'my-article'

Accessing Parameters

📥 Request

The Request class provides complete access to HTTP request data.

Path and Method

Query Parameters

HTTP Headers

Cookies

Body (POST/PUT/PATCH)

Utility Methods

Customization for Tests

📤 Response

The Response class allows you to create and send HTTP responses.

Simple Response

JSON Response

Custom Headers

Available Methods

🔌 Dependency Injection

The Router now supports dependency injection via a Container. This allows automatic injection of dependencies into controllers and middlewares.

Configuration with Container

Controllers with Dependencies

Note: If no Container is set, the Router instantiates controllers directly with new.

🛡️ Middlewares

Middlewares allow you to execute code before request processing.

Important : The Middleware interface has been improved. The handle() method now returns ?Response instead of void. If a middleware returns a Response, execution stops and that response is returned. If it returns null, execution continues with the next middleware.

Global Middlewares

Route-Specific Middlewares

Available Middlewares

CorsMiddleware

AuthMiddleware

RoleMiddleware

LoggingMiddleware

Create a Custom Middleware

⚠️ Error Handling

The router automatically handles common errors:

Customize Error Handling

📚 API Reference

Router

registerRoutes(string $controller): void

Registers all routes of a controller.

addMiddleware(Middleware $middleware): void

Adds a global middleware.

handle(Request $request): Response

Processes a request and returns the response.

getRoutes(): array

Returns all registered routes (debug).

getRouteByName(string $name): ?array

Returns a route by its name.

url(string $name, array $params = [], array $queryParams = []): ?string

Generates a URL from a route name and its parameters.

group(string $prefix, array $middlewares, callable $callback): void

Creates a route group with a prefix and common middlewares.

Request

Main Methods

Response

Constructor

Static Methods

Instance Methods

🔗 Integration with Other Packages

Integration with core-php

core-php automatically includes php-router. The router is accessible via Application::getRouter().

Integration with auth-php

Use authentication middlewares with php-router.

Standalone Usage

php-router can be used independently of all other packages.

🔗 URL Generation

The router allows you to generate URLs from route names, which facilitates maintenance and avoids hardcoded URLs.

Simple URL Generation

URL Generation with Query Parameters

Usage in Responses

Note: To use $router in your controllers, you can inject it via a dependency container or pass it as a parameter.

💡 Complete Examples

Example 1: Complete REST API with Groups

Example 2: Web Application with Authentication and Groups

🧪 Tests

The package includes a complete test suite. To run the tests:

📝 License

MIT License - See the LICENSE file for more details.

🤝 Contributing

Contributions are welcome! Feel free to open an issue or a pull request.

📧 Support

For any questions or issues, please open an issue on GitHub.

💝 Support the project

If this bundle is useful to you, consider becoming a sponsor to support the development and maintenance of this open source project.


Developed with ❤️ by Julien Linard


All versions of php-router with dependencies

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

Loading the files please wait ...