PHPackages                             sigareng/nam - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. sigareng/nam

ActiveLibrary[Framework](/categories/framework)

sigareng/nam
============

Nam is a open source PHP router. super small, fast, and powerful, micro-framework

v1.0.0(4y ago)134MITPHPPHP &gt;=7.2

Since Apr 14Pushed 4y ago1 watchersCompare

[ Source](https://github.com/sigareng/Nam)[ Packagist](https://packagist.org/packages/sigareng/nam)[ Docs](https://github.com/sigareng/nam)[ RSS](/packages/sigareng-nam/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)DependenciesVersions (2)Used By (0)

Nam
===

[](#nam)

Nam is a open source PHP router. super small, fast, and powerful. micro-framework

### Install

[](#install)

Just Copy clone or copy nam.php in directory

> wget

If you have Composer, just include Nam as a project dependency in your `composer.json`. with

```
require: {
    "sigareng/nam": "dev-master"
}

```

or with command line

```
composer require sigareng/nam:dev-master

```

### Examples Used

[](#examples-used)

First, `use` the `sigareng\Nam` namespace:

```
use sigareng\Nam\Nam;
```

if clone child folder dont forget to call

```
Nam::setbase('');

```

Nam is not an object, so you can just make direct operations to the class. Here's the Hello World:

```
Nam::get('/', function() {
  echo 'Hello world!';
});

Nam::dispatch();
```

> like this

index.php

```
require __DIR__.'/nam.php';
use sigareng\Nam\Nam;

Nam::get('/', function() {
  echo 'Hello world!';
});

Nam::dispatch();
```

with composer

```
require './vendor/autoload.php';

use sigareng\Nam\Nam;

Nam::get('/', function() {
  echo 'Hello world!';
});

Nam::dispatch();
```

`Nam` also supports lambda URIs, such as:

```
(:any) -> [^/]+
(:num) -> [0-9]+
(:all) -> .*

```

```
Nam::get('/(:any)', function($slug) {
  echo 'I get : ' . $slug;
});

Nam::dispatch();
```

You can also make requests for HTTP methods in Nam, so you could also do:

```
Nam::get('/', function() {
  echo 'Im a GET request!';
});

Nam::post('/', function() {
  echo 'Im a POST request!';
});

Nam::any('/', function() {
  echo 'I can be both a GET and a POST request!';
});

Nam::dispatch();
```

View Renderer
-------------

[](#view-renderer)

> assumed directory layout

```
.
├── nam.php
├── view
    ├── head.php
    ├── body.php
    ├── footer.php
└── index.php

```

```
require __DIR__.'/nam.php';
use Nam\Nam;

Nam::get('/', function() {
  Nam::render('./view/head.php');
  Nam::render('./view/body.php');
  Nam::render('./view/footer.php');
});

Nam::dispatch();
```

```
Nam::get('/(:num)', function($val) {
  $age['alice']=$val;
  Nam::render('./hi.php',$age);
//   echo '' . print_r(get_defined_vars(), true) . '';
});
```

and inside hi.php

```
echo 'hola, age alice is :'.$data['alice'];
```

> to test after clone this repository, run `php -S localhost:8080` and goto `http://localhost:8080/Example/` on browser

Error Handling
--------------

[](#error-handling)

You can pass a message into the exception that will be displayed in place of the default message on the 404 page.

```
Nam::error(function() {
  echo '404 :: Not Found';
});
```

If you don't specify an error callback, Nam will just echo `404`.

---

to direct properly without `.php` extension example [configuration files](https://github.com/sigareng/Nam/blob/master/config).

Example passing to a controller instead of a closure
----------------------------------------------------

[](#example-passing-to-a-controller-instead-of-a-closure)

---

index.php:

```
require __DIR__.'/nam.php';

use Nam;

Nam::get('/', 'Controllers\see@index');
Nam::get('page', 'Controllers\see@page');
Nam::get('view/(:num)', 'Controllers\see@view');

Nam::dispatch();
```

see.php:

```
