PHP Router
Setup
To install this package you can do this by the following command:
composer require remcosmits/php-routing
This packages uses composer require remcosmits/error-page
for a custom error page to make the error more clear. It gives you a preview where the error starts in your code. You don't have to install this on your own it comes with this framework.
Usage
Make a file called index.php
in your root map with the following content:
// Require composer autoloader
require_once __DIR__ . '/vendor/autoload.php';
// Create Application/PHP Router instance
$app = new PHPRouter\Application();
// Define all routes
$app->get('/',function ($content) {
$content->template('home')->title('Home page');
});
// how to use the middleware function
// you can use a function that gives boolean back as middleware rule
$app->middleware(isLoggedIn(), function ($app) {
// this route is now only available when there is a user logged in
$app->get('/',function ($content) {
$content->template('home')->title('Home page');
});
});
// Run it!
$app->run();
htaccess
If your project is not directly in the root map of your server your can change the RewriteBase
to the map that your project is in. You need to change your config define('ROOT_PATH', 'YOUR_NEW_ROOT_PATH')
aswel.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php [QSA,L]
Config
You can override the config for your project. To do this you can place on of these before you make an instance of the application class
// debug mode (show errors):
// default true
define('DEBUG_MODE', true);
// the root path of your project:
// default value is $_SERVER['DOCUMENT_ROOT']
define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT']);
// path for your pages:
// default '/vendor/remcosmits/php-routing/templates/content-templates/'
define('TEMPLATE_PATH', '/vendor/remcosmits/php-routing/templates/content-templates/');
// file for your html head structure:
// default DOCUMENT_ROOT.'/vendor/remcosmits/php-routing/templates/head.php'
define('HEAD_PATH', '/vendor/remcosmits/php-routing/templates/head.php');
// file for your html footer structure:
// default DOCUMENT_ROOT.'/vendor/remcosmits/php-routing/templates/footer.php'
define('FOOTER_PATH', '/vendor/remcosmits/php-routing/templates/footer.php');
// company name will be used to display in url:
// default Remco Smits
define('COMPANY_NAME','Remco Smits');