/Router

Mvc Router - PHP

Primary LanguagePHP

MVC: Router Implementation

Implementing a mvc routing class

Examples


```php use Router\Router;

$tmp = new Router;

//Adds a GET route to a closure $tmp->get('', function() { print "Default Route :P"; });


<p>Routing a request to a function:</p>

```php
function myFunction() {
    print "Hello World!";
}

//Adding a route to a defined function
$tmp->get('/sayHello', 'myFunction');

Defining controllers to handle routes:

$tmp->get('/html', '\Controller\Controller@html');
$tmp->post('/html', '\Controller\Controller@html');

Defining parameters in route:

$tmp->get('/controller/news/{id}', '\Controller\Controller@news');

In this case, the router will assign the {id} value as an argument to the 'news' method at 'Controller' class:

namespace Controller;
class Controller
{
    public function news($id)
    {
        print "ID sent: $id";
    }
}

Finally, you can define rules to especify the variable type:

$tmp->get('/controller/news/{id}', '\Controller\Controller@findNewsById', array("id"=>"numeric"));
$tmp->get('/controller/news/{name}', '\Controller\Controller@findNewsByName', array("id"=>"letters"));

Current Available Rules:

  • alphanumeric - [0-9a-zA-Z]
  • alphanumeric_underscore - [0-9a-zA-Z_]
  • alphanumeric_full - [0-9a-zA-Z_\-\+]
  • numeric - [0-9]
  • letters - [a-zA-Z]