NanoController - A KISS controller built for speed

NanoController is an extremely simple and fast controller that uses the URI and not a routing table.

Why NanoController?

Traditional MVC frameworks use a routing table, which ends up being difficult to maintain and not obvious as to what the resulting URI is or what classes end up being invoked. Routing tables are a level of indirection that are really not needed for projects that define their own pathing, which is basically any business app behind a sign in page. In addition to requiring a complex routing table, the routing table takes up memory and execution time to resolve the route. NanoController has virtually no memory footprint and a very fast lookup algorithm. Reduced memory usage and fast execution times are especially important with an interpreted language such as PHP. Time and memory are cumulative, and best avoided where ever possible.

KISS - Keep It Simple Stupid

The NanoController maps namespaces, classes and methods directly to the URI and dispenses with a routing table. The result is an easy to understand namespace and classs structure that exactly matches your URI. The URI tells you exactly where the class lives, and the class tells you the exact URI. No need to check a routing table to know what is invoked when.

Naming Conventions

NanoController follows standard naming conventions to figure out what namespace, class and method to call. Namespaces and classes should use Studly Case, capitalized first letter and capitalized letter of every word. Methods should follow camelCase, where the first letter is lowercase, with subsequent word's first letter upper cased, although this is not required, as PHP method names are case insensitive (unfortunately). NanoController uses the first lower case segment as the method name. The preceding segments form the namespace and class.


You can pass parameters with URI segments past the first lower case segment (which is the method name to call in the class) by simply specifying additional segments. NanoController uses method parameter types to cast the parameters to the right types. You can specify any scalar (bool, int, float, string), and NanoController will cast it to the correct type. If your parameter type is a class, NanoController will instantiate the class and pass the constuctor a string representation of the corresponding URI segment. If you specify array as the type, NanoController will pass all subsequent URI segments as an array of strings. No other parameters will be passed after an array parameter.

Method Call

NanoController will instantiate the class (which must impliment NanoClassInterface) and call the specified method and pass any provided parameters. The run method returns the instantiated class with the specified method run. It is your job to deal with the object after that. Generally you would just call its output function (__toString normally) and return a completed page, but it is up to you.

Landing Pages

If a valid method is not found, but the class is, NanoController will attempt to call the default missing method if explicitly defined by calling setMissingMethod. This allows for a default landing page if a method is not called. If a default method is not specified, NanoController continues up the URI tree looking for a default method. If no class and default method are found, then the missing page is returned.

Missing Class

Users are prone to not typing in URIs exactly, so if NanoController can not find a class, or a class and method to instantiate, it will return a missing class of App\Missing, which can be overridden if needed by calling setMissingClass. If the namespace and class do not exist, the missing class will be returned. If the class exists, the missing method will be tried, and if not found, the missing method will be searched for back up the URI tree.

Examples (assuming default App namespace)

/Account/Users/edit/4321\App\AccountUsersedit(int $id)(int)4321
/Account/Users/update/4321\App\AccountUsersupdate(\Model\User $user)new \Model\User('4321')
/Account/Users/friends/4321/5810/23704/17639/699382\App\AccountUsersfriends(int $id, array $friends)(int)4321, ['5810', '23704', '17639', '699382']
/Account/Users/Fiends\AppMissing__construct(NanoController)none (class not defined)

You can change the root namespace from App to anything by calling setRootNamespace('App\Controller') for example.

What about GET, POST, PUT, and DELETE?

Unlike complicated routing tables, NanoController leaves the handling of HTTP methods to the class. Since browsers only support GET and POST, you have to hack PUT and DELETE methods anyway, so just deal with the HTTP method how ever you see fit.

Cloneable Instantiable
public __construct ( string $uri )

Construct the controller. You generally pass $_SERVER['REQUEST_URI'], but it is up to you.

public getErrors () : array
  • return array of errors found, for diagnostic information
public getFiles () : array
public getGet () : array
public getInvokedPath () : string

Returns the URI path that was finally loaded

public getPost () : array
public getUri () : string

Returns the URI provided in the constructor

public run () : PHPFUI\NanoClassInterface

Run the controller and execute the class and method indicated by the URI

  • return PHPFUI\NanoClassInterface object instantiated class with the appropriate method called, except if the missing class is returned, then just the constructor has been called.
public setFiles ( array $files = [ ] ) : self
public setGet ( array $get = [ ] ) : self
public setMissingClass ( string $missingClass = 'App\Missing' ) : self

If no class::method is found in the URI, return an instance of this class.

public setMissingMethod ( string $missingMethod = '' ) : self

If a class is found, but a method is not, then try calling this missing method. If no missing method is defined, go back up the tree looking for this method.

public setPost ( array $post = [ ] ) : self
public setRootNamespace ( string $namespace = 'App' ) : self

Namespace prefix for your classes so they don't have to be in the root namespace

private $errors
private $files
private $get
private $invokedPath
private $missingClass
private $missingMethod
private $post
private $rootNamespace
private $uri
private invokeClassMethod ( array $class , string $method , array $parts = [ ] , int $index = 0 ) : PHPFUI\NanoClassInterface

Test if the class and method exists, and if so, return the instantiated class with the method called

private punt ( array $classParts ) : PHPFUI\NanoClassInterface

We can't find a Class\Method pair, so just find a class and check if it has a landing page if defined, else go up one level.

© 2020 Bruce Wells
Search Namespaces \ Classes
Configuration Numbers (0-9.) only