Download the PHP package mrf0o/php-router without Composer

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

This router is designed to help you easily and efficiently handle HTTP requests and responses in your PHP applications. It is inspired by the popular Laravel framework, and aims to provide a similar experience and functionality.

Getting Started

Installation

You may use composer to intall MrF0o/php-router by running this command:

Setup

After installing the package you may run your app either via the php command line or via a reverse-proxy server such as Apache.

Method 1: PHP CLI

create an index.php for example in the root of your project (you can name whatever you want) with this content:

This command will run your app on port 8888. And if everything went correctly, visiting http://localhost:8888 on your browser should show the text 'Hello World'.

Method 2: Reverse proxy

Here you can find a .htaccess file example that you can use with the rewrite rules needed for this router to run correctly. You can use the same example code from the previous method and make sure to place the file somewhere under the document root that Apache expects (Generally htdocs).

Apache by default uses index.php as the main file so it will serve it by default. but if you want to use this router globally within you project you want all traffic to point to the index.php even tho the user tried to access a different folder withing you project.

Also, notice the public/ directory in the rewrite rule. This rule redirects all asset requests to the public folder. You can rename this folder to something else, like assets, if you'd prefer. Just make sure to update the rewrite rule accordingly.

Note: you need to change index.php to the file that contains the Router::run() call.

[!IMPORTANT] This router does not support subfolders yet. You may need extra configurations in your .htaccess to make it work.

The Router class

After the installation is complete you can start using the router by including The Router class. for example this route will fire whenever /hello is hit:

You can see that each request is represented as a static method of the Router class, thus you can change the get with each of these methods: post, put, patch and delete.

Make sure to run the Router using the run static method.

Route parameters

To capture a segment in your url you can use route parameters, these parameters will be passed to the handler function in order.

optional parameters aren't implemented yet, so this route will be matched only if the parameter {name} is present, otherwise it will give a 404 error

Regular Expression Constraints

Sometimes, you may need to constrain a parameter using regular expression, you can do this using the where method on the Route instance.

Named Routes

You may give names to your routes using the name method on Route instance, this will make it easier to reference your routes elsewhere in your code using the route helper method later.

Grouping routes

Also, you can create route groups using the group method of the router class, each Route registered in the callback will share the properties passed to the group method.

Route redirects

You can redirect a route to another route using the redirect method on the Router class.

by default the redirect will be a 302 redirect, but you can change that by passing the status code as the third argument.

or if you would like a permanent redirect you can use the permanentRedirect method, this will send a 301 redirect.

Generating URLs

You can generate URLs for your routes using the route helper method, this method accepts the name of the route and a variable count of parameters to be passed to the route.

Middlewares

Middlewares are a great way to filter requests before they reach your route handler, in this router library Middlewares are represented as classes that Overrides the handle method in the \Mrfoo\PHPRouter\Middleware class.

the handle method will be called before the route handler, so you can do any checks you want and redirect the user if needed.

Then you can use the middleware in your routes like this:

and you may assign multiple middlewares to a route like this:

you can override the terminate method to do any cleanup after the route handler is called.

Quick Example

Here I used UserController class as an example to demonstrate, the other convention to use route handlers besides the callback function.

TODO


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 mrf0o/php-router contains the following files

Loading the files please wait ....