jossuer/auto_router

Php router lib

v1.0.1 2022-03-04 21:33 UTC

This package is auto-updated.

Last update: 2024-09-10 00:44:50 UTC


README

AutoRouter is a small lib for routing in PHP object-oriented projects

Prerequisitos

  • PHP 5.6 or greater
  • URL Rewriting
  • Class controllers project structure

Installation

composer require jossuer/auto_router

Example Project Structure

Models/
    ...
Views/
    ...
Controllers/
	MainController.php
	HomeController.php
	OtherController.php
	...
.htaccess		
index.php

Apache URL Rewriting

.htaccess

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

Usage

index.php

<?php
require_once 'vendor/autoload.php';

$root_path = "";
$default_controller = "Home";
$default_method = "index";
$namespace = "\\Controllers\\";
$controllerSuffix = "Controller";

use AutoRouter\Router;

$router = new Router();
$router->setRootPath($root_path)
    ->setDefaultController($default_controller)
    ->setDefaultMethod($default_method)
    ->setNamespace($namespace)
    ->setControllerSuffix($controllerSuffix); 
$router->exec();

Example Controller

HomeController.php

class HomeController
{
    function loginAction(){
        ...
    }
    
}

Then you can access this controller by: http://your_server.com/Home/login