Skip to content

Controllers

Prasad Nayanajith edited this page Oct 12, 2017 · 1 revision

What is a controller?

A Controller is simply a class file that is named in a way that can be associated with a URI.

Consider this URI: example.com/blog/posts

In the above example, DynaPort X would attempt to find a controller named posts.php inside the module blog and load it.

When a controller's name matches the second segment of a URI, it will be loaded.

Create a controller

Let's create a simple controller so you can see it in action. Using your text editor, create a file called posts.php, and put the following code in it:

<?php

class BlogPostsController extends Controller {

    public function public_index(){
        echo 'Hello World!';
    }

}

?>

Now create a folder as controllers inside your module folder application/modules/blog/.

Then save the file to your application/modules/blog/controllers/ folder.

Now visit the your site using a URI similar to this: example.com/blog/posts

If you did it right, you should see Hello World!.

Also, always make sure your controller extends the parent controller class so that it can inherit all its functions.

Naming a controller

In DynaPort X, there is a specific naming pattern that you must use to name classes.

To avoid the confusion, consider the following abbreviations.

A - Module Name
B - Controller Name

Now the class naming method is: ABController.

For an example, consider the module is Blog and the controller is Posts. In that case the class name should be: BlogPostsController.

Important: Camel case naming convention must be followed.

Saving a controller

If you consider the previous abbreviation the file name of the class should be a.php. As of the previous example, it should be: posts.php and must be saved inside your application/modules/blog/controllers/ folder.

Default controller

By default DynaPort X consider ModuleIndexController as the default controller. That means if the module name is blog, the default controller the framework load if nothing is specified in the URI is: BlogIndexController.

Ex: When only the module name is specified: example.com/blog

Functions

Create a function

In the previous example the function name is public_index(). Another way to show your "Hello World" message would be this: example.com/blog/posts/index

The third segment of the URI determines which function in the controller gets called.

Let's try it. Add a new function to your controller:

<?php

class BlogPostsController extends Controller {

    public function public_index(){
        echo 'Hello World!';
    }

    public function public_comments(){
        echo 'My first comment!';
    }

}

?>

Now load the following URI to see the comment function: example.com/blog/posts/comments

You should see your new message.

Default Function

As for the functions, DynaPort X consider public_index as the default function. So if no function is being specified in the URI, the framework will load the public_index function.

Ex: When only the module and controller is specified: example.com/blog/posts

Function Prefixes

If you look at the function name of your first controller, you'll that it has the prefix public_. This is the request method declaration of the DynaPort X framework.

Functions that are starting with the prefix public_ are accessible through any HTTP request method no matter whether it's GET, POST or something else.

For an example, if you change public_ to post_, you can access the URI which looks similar to this example.com/blog/posts only through a POST request.

Consider the following Hello World controller.

<?php

class BlogPostsController extends Controller {

    public function public_index(){
        echo 'Hello World!';
    }

    public function post_index(){
        echo 'Hello World!';
        echo 'This is a POST request.';
    }

    public function index(){
        echo 'Hello World!';
        echo 'I am not publicly accessible.';
    }

}

?>

Look at the first two functions. Now if you send a POST request to the same URI as mentioned earlier, you'll see Hello World! This is a POST request. output. If you send a GET or any other request, you should see just Hello World!.

Prioritization

What happens in the previous example is, the framework checks if there is a specific function for the exact request method. (post_ for POST, delete_ for DELETE). If there is, the framework loads it right away. If not, the framework looks for a general function (public_) to load.

Conclusion: Dedicated (prefixed) function first, general function later.

Private Functions

If you look at the last function of the previous example, you'd see that there is no prefix. Functions like these without any request method specified are considered private. They cannot be accessed publicly via an HTTP request but only internally through the same or other controllers (HMVC).

Passing URI Segments

If your URI contains more then three segments they will be passed to your function as parameters.

For example, lets say you have a URI like this: example.com/blog/posts/comments/123/random

Your function will be passed URI segments 4 and 5 ("123" and "random"):

<?php

class BlogPostsController extends Controller {

    public function public_index(){
        echo 'Hello World!';
    }

    public function public_comments($id,$sort){
        echo $id;
        echo $sort;
    }

}

?>