Fat-Free Framework – 6. Organising controllers in subfolders, routes and namespacing

Created at: October 20, 2016; Last update: October 20, 2016

The namespacing allows us to organize our scripts so that they won’t have conflicts if the classes have the same name and are in different directories. Also, we can group the controllers inside subdirectories.

So how do we do this?

Let’s assume we will have more controllers that we want to group inside the “Users” directory.

For this we will create a “Users” directory inside our “App/Controllers/” directory. Now let us create a Test controller (Test.php) inside our newly created directory:

Reading from the documentation we find out that we need to attach this file to a namespace so let us define it:

Now, all we have to do is create a route towards this new controller. For this we go to our routes.cfg and add a new line:

Now if you visit yoursite.com/testingusers you should receive a “hello” from our framework.

Done.