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. NanoController will append the request method verb (in Studly Case) to the method name. If this method is not found, the base method name is tried. The method must be public.


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. Any parameters not specified will use the PHP default, so it is suggested that all parameters for the method have default values.

Method Call

NanoController will instantiate the class (which must impliment \PHPFUI\Interfaces\NanoClass) 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.

Home Page

If a blank URI (or \) is provided, it will return a class of App\Missing, unless setHomePageClass has been called. No method will be called on the HomePage class.

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.

public __construct(string $uri)

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

public getErrors() : array
public getFiles() : array
public getGet() : array
public getInvokedPath() : string

Returns the URI path that was finally loaded

public getPost() : array
public getRequestMethod() : string

Return the StudlyCased request method name

public getUri() : string

Returns the URI provided in the constructor

public run() : PHPFUI\Interfaces\NanoClass

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

  • return phpDocumentor\Reflection\Types\Object_ 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 = []) : static
public setGet(array $get = []) : static
public setHomePageClass(string $homePageClass = 'App\HomePage') : static

If no URI (or /) is provided, return an instance of this class.

public setMissingClass(string $missingClass = 'App\Missing') : static

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

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

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 = []) : static
public setRootNamespace(string $namespace = 'App') : static

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

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

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

private punt(array $classParts, array $parameters, int $count) : PHPFUI\Interfaces\NanoClass

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.

© 2023 Bruce Wells
Search Namespaces \ Classes