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:

App/Controllers/Users/Test.php

PHP

1

2

3

4

5

6

7

<?php

classTest{

publicfunctionindex()

{

echo'hello';

}

}

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

App/Controllers/Users/Test.php

PHP

1

2

3

4

5

6

7

8

9

10

<?php

namespaceUsers;

classTest{

publicfunctionindex()

{

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:

App/Config/routes.cfg

PHP

1

GET/testingusers=Users\Test->index

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