edit

Skytells Framework Controllers


Overview

Controllers are the heart of your application, as they determine how HTTP requests should be handled.

Tip

This tutorial assumes you’ve downloaded Skytells Framework and installed the framework in your development environment.

In this tutorials we will learn step by step how to create a Skytells Framework Controller.

Difficulty

  • For Seniors : Easy
  • For Beginners : Easy

Definition of Controllers

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

The first thing you’re going to do is set up a controller to handle static pages.
A controller is simply a class that helps delegate work. It is the glue of your web application.

For example, when a call is made to:

1
http://example.com/news/latest/10

We might imagine that there is a controller named “news”.
The method being called on news would be “latest”.
The news method’s job could be to grab 10 news items, and render them on the page.
Very often in MVC, you’ll see URL patterns that match:

1
http://example.com/[controller-class]/[controller-method]/[arguments]

As URL schemes become more complex, this may change.
But for now, this is all we will need to know.

Namespace

Namespace

There is no defined namespace for the base controller.

Creating Controllers

All Controllers must be stored in this folder.

1
Application/Resources/Controllers

Create a file inside the mentioned path to be like :

1
Application/Resources/Controllers/MyController.php

Put with the following code in your controller.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?php
use Skytells\Core;
use Skytells\Runtime\Console;
use Skytells\UI\View;

Class MyController extends \Controller implements IController  {

  function __construct($Ref = "") {
      parent::__construct();
    }

  public function index() {
    // This is the default method for the controller.
    echo "Hello World";
  }
}

Important

Always make sure your controller extends the parent controller class
and call it on the __construct as parent::__construct() so that it can inherit all its methods.

Now visit the your site using a URL similar to this:

1
example.com/MyController

If you did it right, you should see: Hello World! is printed on the screen.

Notice

Notice that the default method index() is automatically called upon accessing the URL.

Methods

In the above example the method name is index().
The “index” method is always loaded by default if the second segment of the URI is empty.
Another way to show your “Hello World” message would be this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
use Skytells\Core;
use Skytells\Runtime\Console;
use Skytells\UI\View;

Class MyController extends \Controller implements IController  {

  function __construct($Ref = "") {
      parent::__construct();
    }

  public function index() {
    // This is the default method for the controller.
    echo "Hello World";
  }

  public function wow() {
    // This is the default method for the controller.
    echo "Hello World comes from wow";
  }
}

Now load the following URL to see the comment method:

1
example.com/MyController/wow/

You should see your new message.

Passing URI Segments to your methods

If your URI contains more than two segments they will be passed to your method as parameters.

For example, let’s say you have a URI like this:

1
example.com/Users/details/Hazem/123

Your method will be passed URI segments 3 and 4 (“Hazem” and “123”):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
use Skytells\Core;
use Skytells\Runtime\Console;
use Skytells\UI\View;

Class Users extends \Controller implements IController  {

  function __construct($Ref = "") {
      parent::__construct();
    }

  public function index() {
    // This is the default method for the controller.
    echo "Hello World";
  }

  public function details($username, $id) {
    // This is the default method for the controller.
    echo $username;
    echo $id;
  }
}

Important

Notice here we've created another controller under name of Users contains the a method named details.
If you are using the URI Routing feature, the segments passed to your method will be the re-routed ones.

Defining a Default Controller

Skytells Framework can be told to load a default controller when a URI is not present, as will be the case when only your site root URL is requested. To specify a default controller, open your Application/Misc/Config/Routes.php file and set this variable:

1
2
3
<?php
$Routes["CONFIG"]["DEFAULT_CONTROLLER"] = "Home"; // <- Default Controller
$Routes["CONFIG"]["DEFAULT_METHOD"] = "index";    // <- Default Method

Remapping Method Calls

Of course you can remap controllers and method calls by adding new routes to Application/Misc/Config/Routes.php

That’s it!

That, in a nutshell, is all there is to know about controllers.