Download the PHP package focus237/the-router without Composer

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

# The Router Documentation

Installation

Add the latest version of the-router project running this command.

composer require focus237/the-router

Features

Server Setup

Setting up Apache

Nothing special is required for Apache to work. We've include the .htaccess file in the root folder. If rewriting is not working for you, please check that the mod_rewrite module (htaccess support) is enabled in the Apache configuration.

.htaccess example

Below is an example of an working .htaccess file used by the-router.

Simply create a new .htaccess file in your root directory and paste the contents below in your newly created file. This will redirect all requests to your index.php file. According your index.php is in public folder, you'll write something like this

Setting up Ngninx

If you are using Nginx please make sure that url-rewriting is enabled.

You can easily enable url-rewriting by adding the following configuration for the Nginx configuration-file for the demo-project.

Configuration

Create a new file, name it routes.php and place it in your library folder. This will be the file where you define all the routes for your project.

WARNING: NEVER PLACE YOUR ROUTES.PHP IN YOUR PUBLIC FOLDER!

In your index.php require your newly-created routes.php and call the $router->run() method. This will trigger and do the actual routing of the requests.

It's not required, but you can set TheRouter::setDefaultNamespace('\Controllers\Path'); to prefix all routes with the namespace to your controllers. This will simplify things a bit, as you won't have to specify the namespace for your controllers on each route. The default namespace for controllers is App\Controller .

This is an example of a basic index.php file:

<?php

use TheRouter\Router\Router;

$router = new Router();

/* Load external routes file */
require_once 'routes.php';

/**
* The default namespace for route-callbacks, so we don't have to specify it each time.
* Can be overwritten by using the namespace config option on your routes.
*/
TheRouter::setDefaultNamespace('App\Controller');

/* Start the routing */
$router->run();

Routes

Remember the routes.php file you required in your index.php? This file be where you place all your custom rules for routing.

Basic Routing

Below is a very basic example of setting up a route. First parameter is the url which the route should match - next parameter is a Closure or callback function that will be triggered once the route matches.

$router->get('/', function() {
    return 'Hello world';
});

or you can call controller action by using ControllerName@Function :

$router->get('/', 'DefaultController@index');

Available methods

Here you can see a list over all available routes:

$router->get($url, $callback, $name);
$router->post($url, $callback, $name);
$router->resource($name, $controller);

$name is the route name. See named routes section for more informations.

Routes parameters

You'll properly wondering by know how you parse parameters from your urls. For example, you might want to capture the users id from an url. You can do so by defining route-parameters.

$router->get('/user/:id', function ($id) {
    return 'User with id : ' . $id;
});

You may define as many route parameters as required by your route:

$router->get('/posts/:post/comments/:comment', function ($post, $comment) {
    // ...
});

Regular expression constraints

You may constrain the format of your route parameters using the where method on a route instance. The where method accepts the name of the parameter and a regular expression defining how the parameter should be constrained:

$router->get('/user/:name', function ($name) {

    // ... do stuff

})->where('name', '[A-Za-z]+');

$router->get('/user/:id', function ($id) {

    // ... do stuff

})->where('id', '[0-9]+');

$router->get('/user/:id/:name', function ($id, $name) {

    // ... do stuff

})->where(['id' => '[0-9]+', 'name' => '[a-z]+']);

Named Routes

Named routes allow the convenient generation of URLs or redirects for specific routes. There is two ways for defining route name.

Chaining

You may specify a name for a route by chaining the name method onto the route definition (in this example, profile is the route name):

$router->get('/user/profile', function () {
    // Your code here...
})->name('profile');

Name parameter

You may specify a name for a route by adding it next to the callback of the route (in this example, profile is the route name):

$router->get('/user/profile', function () {
    // Your code here...
}, 'profile');

Generating URLs to named routes

Once you have assigned a name to a given route, you may use the route's name when generating URLs or redirects via the global $router->url('route_name') function:

// Generating URLs...
$url = $router->url('profile');

If the named route defines parameters, you may pass the parameters as the second argument to the url function. The given parameters will automatically be inserted into the URL in their correct positions:

$router->get('/user/:id/profile', function ($id) {
    //
})->name('profile');

$url = $router->url('profile', ['id' => 1]);

All versions of the-router with dependencies

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

Loading the files please wait ....