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

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:

<?php
class Test {
  public function index()
  {
    echo 'hello';
  }
}

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

<?php
namespace Users;

class Test {

  public function index()
  {
    echo 'hello';
  }
}

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:

GET /testingusers=Users\Test->index

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

Done.

Leave a Reply

Your email address will not be published. Required fields are marked *

No spam? * Time limit is exhausted. Please reload CAPTCHA.