1. Go to this page and download the library: Download dcp/router library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dcp / router example snippets
useDCP\Router;
$mvcRouter = new Router\MvcRouter();
$restRouter = new Router\RestRouter();
useDCP\Router\MvcRouter;
classTestController{
publicfunctionindexAction($arg = null){
echo__METHOD__ . ', ' . $arg;
}
}
classHomeController{
publicfunctiontestAction(){
echo__METHOD__;
}
}
$router = new MvcRouter();
$router->dispatch('/test/index/hello');
// This will output "TestController::indexAction, hello"
$router->dispatch('/home/test');
// This will output "HomeController::testAction"
useDCP\Router\RestRouter;
classUsersController{
publicfunctionget(){
echo__METHOD__;
}
publicfunctionpost($arg = null){
echo__METHOD__ . ', ' . $arg;
}
}
$router = new RestRouter();
$router->dispatch('/users/hello', 'post');
// This will output "UsersController::post, hello"
$router->dispatch('/users', 'get');
// This will output "UsersController::get"
useDCP\Router\MvcRouter;
classIndexController{
publicfunctionindexAction(){
echo__METHOD__;
}
}
classHomeController{
publicfunctionindexAction(){
echo__METHOD__;
}
}
$router = new MvcRouter();
$router->dispatch('/');
// This will output "IndexController::indexAction"
$router->dispatch('/home');
// This will output "HomeController::indexAction"
useDCP\Router\RestRouter;
classIndexController{
publicfunctionget(){
echo__METHOD__;
}
}
classHomeController{
publicfunctionget(){
echo__METHOD__;
}
publicfunctionpost(){
echo__METHOD__;
}
}
$router = new RestRouter();
$router->dispatch('/');
// This will output "IndexController::get"
$router->dispatch('/home');
// This will output "HomeController::get"
$router->dispatch('/home', 'post');
// This will output "HomeController::post"
useDCP\Router\MvcRouter;
$router = new MvcRouter(); // RestRouter can be used here, too.try {
$router->dispatch('/home');
} catch (NotFoundException $e) {
echo'Could not find /home!!';
}
// index.phpuseDCP\Router\MvcRouter;
$router = new MvcRouter();
$router->setControllerPrefix('App\Controller');
$router->dispatch('/test/hello');
// This will output "Hello, world!"