- Set-up your .htaccess file. For development on localhost using xamp/wamp. You can copy this sample htaccess code below into notepad and save the file as .htaccess
Then, simply change index.php to whatever script you want to use as main entry PHP script/file.
#Redirect All Requests To index.php Using .htaccess
RewriteRule ^(.*)$ index.php [QSA,L,NC]
#Then, Redirect /index.php to / #(optional, but recommended if the request contains index.php)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php [NC]
RewriteRule ^index.php/?(.*)$ $1 [R=301,L] - Copy the .htaccess file into your web app folder. For XAMP users. It would be moved into your the app folder you’re creating inside your htdocs. So, if your app name is
myapp
. The above htaccess file would insidemyapp
- Lastly, If you’re using the Emma Framework, http-manager will be installed as a required dependency and you skip this step. Otherwise, to install http manager inside your app folder via composer:
composer require emma/http-manager
Register your Class/Methods/Function via the RouteRegistry singleton. You can do this inside your index.php entry file.
For example:
RouteRegistry::getInstance()->register(UserController::Class);
OR
create a config array and invoke setRoutable method of RouteRegistry. e.g:
<?php //controllers.registry.php file
//include classes as needed...
return [
IndexController::class,
LoginController::class,
AuthController::class,
...
];
?>
RouteRegistry::getInstance()->setRoutables((array)include controllers.registry.php);
That’s it. No hassle nor stress. Now you can specify your routing as shown in the example image below. You can see the repo for a quick tour/info on the options available.
Example – Code Sample
<?php
include dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . "autoloader.php"; //composer autoloader
// <!-- Controller Class file -->
use Emma\Http\Mappings\RequestMapping;
use Emma\Http\Mappings\PostMapping;
use Emma\Http\Mappings\GetMapping;
use Emma\Http\Request\Method;
#[RequestMapping(routes: '/index', httpRequestMethod: [Method::POST, Method::GET])]
class IndexController
{
/**
* Full Routing to this method is: '/index/login' -> Class level routing plus the method level
* Class Method can only be accessed via HTTP - POST
*/
#[PostMapping('/login')]
public function login()
{
}
/**
* Class Method can be accessed via HTTP - POST and GET
*/
#[RequestMapping('/logout', [Method::POST, Method::GET])]
public function logout()
{
}
/**
* Class Method can be accessed via HTTP - GET
* Auto-Map expected url parameter to the method
*/
#[GetMapping('/count-trades/{status:[\w]+}')]
public function countTradesByStatus(string $status)
{
}
/**
* Other Request Mapping Attributes exist....For example:
*
* #[HeadMapping('/head-method-routing')]
* #[PutMapping('/upload')]
* #[PatchMapping('//summary/{id:[0-9]*}')]
* #[DeleteMapping('/delete-all')]
* #[OptionsMapping('/option/')]
*/
}