zippy1981 / php-routing-example
Example of custom PHP routing
v0.3
2019-04-30 15:40 UTC
Requires
- php-di/php-di: ^6.0
Requires (Dev)
- overtrue/phplint: ^1.1
- phpmd/phpmd: @stable
- phpunit/phpunit: ^8.1
- squizlabs/php_codesniffer: 3.*
This package is auto-updated.
Last update: 2024-10-29 05:28:59 UTC
README
CI/CD
Composer stats
Download stats
Using
- Create a project with
composer init
and follow the prompts. composer require zippy1981/php-example-routes
.- Create a namespace to store your controllers.
- Add an index.php with the following:
<?php use \Router\Router; /** * This makes our life easier when dealing with paths. Everything is relative * to the application root now. */ chdir(dirname(__DIR__)); // Decline static file requests back to the PHP built-in webserver if (php_sapi_name() === 'cli-server') { $path = realpath(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); if (__FILE__ !== $path && is_file($path)) { return false; } unset($path); } // Composer autoloading include __DIR__ . '/../vendor/autoload.php'; $container = new DI\Container(); if (! class_exists(Router::class)) { throw new RuntimeException( "Unable to load Router.\n" . "- Type `composer install` if you are developing locally.\n" . "- Type `docker-compose exec web composer install` if you are using Docker.\n" ); } Router::setControllerRoot('YourControllerNamespace'); Router::resource('whatever'); # parent level controller Router::resource('whatever.whenever'); # chile level controller $router = $container->get(Router::class); $router->invokeControllerMethod();